﻿using System;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
using CObject;
using ComponentGo.Web.Schedulers;
using Objects;
using Objects.CMT;

public partial class MeetingCreation : cSecurePage
{

    bool isEditing = false;
    bool isAttendeeEditing = false;
    bool checkEdit = false;
    DataTable dtNewMeetingAttendeeDetials = new DataTable();

    bool isMeetingTimeChanged = false;

    #region Page_Load
    /// <summary>
    /// Page load event.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            dtNewMeetingAttendeeDetials.Columns.Add("Name");
            dtNewMeetingAttendeeDetials.Columns.Add("ISHost");
            dtNewMeetingAttendeeDetials.Columns.Add("Issues");
            dtNewMeetingAttendeeDetials.Columns.Add("CountryRepresent");

            if (!Page.IsPostBack)
            {
                Session["NewMeetingAttendeeDetials"] = null;
                hdnMeetingID.Value = "";

                ListItem defaultItem = new ListItem("Select Event", "0");

                // Binding Events drop down.
                BindEventDropDown(defaultItem);
                defaultItem = new ListItem("Select Meeting Type", "0");

                // Binding Meeting Type drop down.
                BindMeetingTypeDropDown(defaultItem);
                defaultItem = new ListItem("Select Meeting Room", "0");

                // Bind MMeeting Room drop down.
                BindMeetingRoomDropDown(defaultItem);

                // Bind meeting status
                BindMeetingStatusRadioButton();

                // Making all the schedulers invisible.
                schMeetingScheduler.Visible = false;
                lblHost.Visible = false;
                lblHostLegend.Visible = false;
                schSchedulerAvailability.Visible = false;
                schMeetingRoom.Visible = false;
                lblMeetingStatus.Visible = false;
                rblMeetingStatus.Visible = false;
                btnSaveMeeting.Enabled = false;
                btnShowHideRoomSchedule.Disabled = true;
                btnCheckAvailability.Enabled = false;
                btnSchedule.Enabled = false;

                // Checking if the user has selected event in the event search page or not.
                if (Session["EventID"] != null)
                {
                    int eventId = (Int32)Session["EventID"];
                    ddlEventName.SelectedValue = eventId.ToString();
                    ddlEventName_SelectedIndexChanged(this.ddlEventName, EventArgs.Empty);

                    ddlSearchEvent.SelectedValue = eventId.ToString();
                    ddlSearchEvent_SelectedIndexChanged(this.ddlSearchEvent, EventArgs.Empty);
                    btnSearch_Click(this.btnSearch, EventArgs.Empty);
                }
                else
                {
                    ddlEventName.SelectedIndex = 0;
                    ddlSearchEvent.SelectedIndex = 0;
                }
            }

            // Clears all the error messages.
            lblMessage.Text = "";
            lblMailMessage.Text = "";
            vlsSaveAttendeeSchedule.Visible = false;
            vlsMeeting.Visible = true;
            upnlSaveAttendeeSchedule.Update();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "settabs", "SetTabs()", true);
        }
        catch (Exception)
        {
            throw;
        }
    }

    #region BindMeetingRoomDropDown
    /// <summary>
    /// Bind meeting room drop down.
    /// </summary>
    /// <param name="defaultItem"></param>
    private void BindMeetingRoomDropDown(ListItem defaultItem)
    {
        ddlMeetingRoom.Items.Clear();
        ddlMeetingRoom.Items.Insert(0, defaultItem);

        if (Session["EventID"] == null)
        {
            ddlSearchMeetingRoom.Items.Clear();
            ddlSearchMeetingRoom.Items.Insert(0, defaultItem);
        }
    }
    #endregion

    #region BindMeetingStatusRadioButton
    /// <summary>
    /// Bind meeting status radio button.
    /// </summary>
    private void BindMeetingStatusRadioButton()
    {
        MeetingStatus getMeetingSttaus = new MeetingStatus();
        CCollection loColl = new CCollection(new MeetingStatus(), true);
        rblSearchMeetingStatus.DataSource = loColl.DataTable;
        rblSearchMeetingStatus.DataBind();
        rblSearchMeetingStatus.SelectedValue = "1";
    }
    #endregion

    #region BindMeetingTypeDropDown
    /// <summary>
    /// Bind meeting type drop down.
    /// </summary>
    /// <param name="defaultItem"></param>
    private void BindMeetingTypeDropDown(ListItem defaultItem)
    {
        MeetingType meetingTypes = new MeetingType();
        CCollection loColl = new CCollection(new MeetingType(), true);
        DataTable dtMeetingTypeList = loColl.DataTable;
        ddlMeetingType.DataSource = dtMeetingTypeList;
        ddlMeetingType.DataBind();
        ddlMeetingType.Items.Insert(0, defaultItem);

        ddlSearchMeetingType.DataSource = dtMeetingTypeList;
        ddlSearchMeetingType.DataBind();
        ddlSearchMeetingType.Items.Insert(0, defaultItem);
    }
    #endregion

    #region BindEventDropDown
    /// <summary>
    /// Bind event drop down.
    /// </summary>
    /// <param name="defaultItem"></param>
    private void BindEventDropDown(ListItem defaultItem)
    {
        CMTEvent eventsColl = new CMTEvent();
        eventsColl.CurrentEventAdministratorGUI = CurrentUser.GUI;
        CCollection loColl = new CCollection(eventsColl, true);
        DataTable dtEventList = loColl.DataTable;
        ddlEventName.DataSource = dtEventList;
        ddlEventName.DataBind();
        ddlEventName.Items.Insert(0, defaultItem);

        ddlSearchEvent.DataSource = dtEventList;
        ddlSearchEvent.DataBind();
        ddlSearchEvent.Items.Insert(0, defaultItem);


    }
    #endregion

    #endregion

    #region Search Grid Events

    #region wgSearchMeeting_PageIndexChanged
    /// <summary>
    /// Event to change the page index of the search grid and rebind grid.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void wgSearchMeeting_PageIndexChanged(object sender, GridViewPageEventArgs e)
    {
        try
        {
            wgSearchMeeting.PageIndex = e.NewPageIndex;
            BindSearchDetails();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "changeTabs", "ChangeTab(1)", true);
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #region wgSearchMeeting_ItemCommand
    /// <summary>
    /// Event to fire edit for a meeting.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void wgSearchMeeting_ItemCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            Session["NewMeetingAttendeeDetials"] = null;

            if (e.CommandName.Equals("EditMeeting"))
            {
                hdnMeetingID.Value = Convert.ToInt32(e.CommandArgument).ToString();
                lstMeetingAttendee.Items.Clear();
                schSchedulerAvailability.Rows.Clear();
                schSchedulerAvailability.Visible = false;
                schMeetingScheduler.Rows.Clear();
                schMeetingScheduler.Visible = false;
                Session["EventAttendee"] = null;
                Session["MeetingAttendeesDetails"] = null;
                Session["RoomDetails"] = null;
                checkEdit = true;

                // For editing the meeting.
                BindMeetingDetails();
                upnlMeetings.Update();
                ScriptManager.RegisterStartupScript(this, this.GetType(), "changeTabs", "ChangeTab(0)", true);
            }
            else if (e.CommandName.Equals("SendMail"))
            {
                int meetingId = Convert.ToInt32(e.CommandArgument);
                // Get the mail details to be send.
                MeetingMail objSendMeetingMail = new MeetingMail();
                objSendMeetingMail.MeetingID = meetingId;
                objSendMeetingMail.MeetingType = "Active";
                CCollection loColl = new CCollection(objSendMeetingMail, true);
                DataTable dtSendMeetingMail = loColl.DataTable;
                Meeting sendMeetingMail = new Meeting();
                bool mailSent = sendMeetingMail.SendMailToAttendee(dtSendMeetingMail);

                if (mailSent)
                {
                    lblMailMessage.Text = Meeting.MeetingMailSent;
                    lblMailMessage.ForeColor = Color.ForestGreen;
                }
                else
                {
                    lblMailMessage.Text = Meeting.MeetingMailSentFailed;
                    lblMailMessage.ForeColor = Color.Red;
                }

                schSchedulerAvailability.Visible = false;
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

    #region BindMeetingDetails
    /// <summary>
    /// Method to bind all the meeting details for editing meeting.
    /// </summary>
    private void BindMeetingDetails()
    {
        schMeetingScheduler.Rows.Clear();
        schSchedulerAvailability.Rows.Clear();
        schMeetingRoom.Rows.Clear();

        // Bind meeting status
        MeetingStatus getMeetingSttaus = new MeetingStatus();
        CCollection loColl = new CCollection(new MeetingStatus(), true);
        rblMeetingStatus.DataSource = loColl.DataTable;
        rblMeetingStatus.DataBind();

        //int meetingId = (Int32)Session["MeetingID"];
        int meetingId = Convert.ToInt32(hdnMeetingID.Value);
        Meeting getMeeting = new Meeting();
        getMeeting.MeetingID = meetingId;
        loColl = new CCollection(getMeeting, true);
        DataTable dtMeetingDetails = loColl.DataTable;

        if (dtMeetingDetails.Rows.Count > 0)
        {
            int eventId = Convert.ToInt32(dtMeetingDetails.Rows[0]["EventID"].ToString());
            ListItem defaultItem = new ListItem("Select Event", "0");
            // Binding Events drop down.
            ddlEventName.Items.Clear();
            CMTEvent eventsColl = new CMTEvent();
            eventsColl.CurrentEventAdministratorGUI = CurrentUser.GUI;
            loColl = new CCollection(eventsColl, true);
            DataTable dtEventList = loColl.DataTable;
            ddlEventName.DataSource = dtEventList;
            ddlEventName.DataBind();
            ddlEventName.Items.Insert(0, defaultItem);
            ddlEventName.SelectedValue = eventId.ToString();
            ddlEventName_SelectedIndexChanged(this.ddlEventName, EventArgs.Empty);
            ddlEventName.Enabled = false;
            ddlMeetingType.SelectedValue = dtMeetingDetails.Rows[0]["MeetingTypeID"].ToString();
            hdnMeetingType.Value = ddlMeetingType.SelectedValue;
            txtMeetingName.Text = Server.HtmlDecode(dtMeetingDetails.Rows[0]["MeetingName"].ToString());
            txtMeetingDate.Text = Convert.ToDateTime(dtMeetingDetails.Rows[0]["MeetingDate"].ToString()).ToShortDateString();
            txtMeetingDescription.Text = Server.HtmlDecode(dtMeetingDetails.Rows[0]["MeetingDescr"].ToString());
            txtMeetingNote.Text = Server.HtmlDecode(dtMeetingDetails.Rows[0]["MeetingNote"].ToString());
            string[] meetingStartTime = dtMeetingDetails.Rows[0]["MeetingStartTime"].ToString().Split(':');
            ddlMeetingStartTimeHrs.SelectedValue = meetingStartTime[0];
            ddlMeetingStartTimeMns.SelectedValue = meetingStartTime[1];
            string[] meetingEndTime = dtMeetingDetails.Rows[0]["MeetingEndTime"].ToString().Split(':');
            ddlMeetingEndTimeHrs.SelectedValue = meetingEndTime[0];
            ddlMeetingEndTimeMns.SelectedValue = meetingEndTime[1];
            ddlMeetingRoom.SelectedValue = dtMeetingDetails.Rows[0]["RoomID"].ToString();
            ddlMeetingRoom_SelectedIndexChanged(this.ddlMeetingRoom, EventArgs.Empty);
            rblMeetingStatus.SelectedValue = dtMeetingDetails.Rows[0]["MeetingStatusID"].ToString();
            txtMeetingLink.Text = dtMeetingDetails.Rows[0]["MeetingLink"].ToString();
            txtClientName.Text = Server.HtmlDecode(dtMeetingDetails.Rows[0]["ClientName"].ToString());

            // Bind the meeting attendee details.
            MeetingAttendee meetingAttendeeDetails = new MeetingAttendee();
            meetingAttendeeDetails.EventID = eventId;
            loColl = new CCollection(meetingAttendeeDetails, true);
            DataTable dtMeetingAttendeeDetails = loColl.DataTable;
            Session["MeetingAttendeesDetails"] = dtMeetingAttendeeDetails;
            DataView dvMeetingAttendee = new DataView(dtMeetingAttendeeDetails);
            dvMeetingAttendee.RowFilter = "MeetingID = " + meetingId.ToString();
            lstMeetingAttendee.DataSource = dvMeetingAttendee.ToTable();
            lstMeetingAttendee.DataBind();

            if (lstMeetingAttendee.Items.Count > 0)
            {
                lstMeetingAttendee.SelectedIndex = 0;

                foreach (ListItem lsiMeetingAttendee in lstMeetingAttendee.Items)
                    lstEventAttendee.Items.Remove(lsiMeetingAttendee);

                isEditing = true;
                btnSchedule_Click(this.btnSchedule, EventArgs.Empty);

                schMeetingScheduler.Visible = true;
                lblHost.Visible = true;
                lblHostLegend.Visible = true;
                schSchedulerAvailability.Visible = false;
                schMeetingRoom.Visible = true;
                lblMeetingStatus.Visible = true;
                rblMeetingStatus.Visible = true;
                btnCheckAvailability.Enabled = true;
                btnSchedule.Enabled = true;
            }
            else
            {
                schMeetingScheduler.Visible = false;
                lblHost.Visible = false;
                lblHostLegend.Visible = false;
                schSchedulerAvailability.Visible = false;
                schMeetingRoom.Visible = true;
                lblMeetingStatus.Visible = true;
                rblMeetingStatus.Visible = true;
                btnCheckAvailability.Enabled = false;
                btnSchedule.Enabled = false;
            }
        }
    }
    #endregion

    #endregion

    #endregion

    #region ddlMeetingType_SelectedIndexChnaged
    /// <summary>
    /// Event to restrict to edit the meeting type to roundtable table in meeting edit mode..
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlMeetingType_SelectedIndexChnaged(object sender, EventArgs e)
    {
        try
        {
            if (hdnMeetingID.Value != "")
            {
                if (ddlMeetingType.SelectedValue == "1" && hdnMeetingType.Value != "1")
                {
                    ddlMeetingType.SelectedValue = hdnMeetingType.Value;
                    csvMeetingType.ErrorMessage = Meeting.MeetingTypeError;
                    csvMeetingType.IsValid = false;
                }
            }
            else if (hdnMeetingAttendeeCount.Value != "")
            {
                if (ddlMeetingType.SelectedValue == "1")
                {
                    btnSchedule_Click(this.btnSchedule, e);
                }
            }

            if (hdnAllCleasred.Value != "1")
            {
                if (hdnMeetingAttendeeCount.Value == "")
                {
                    schMeetingScheduler.Visible = false;

                    if (schSchedulerAvailability.Rows.Count == 0)
                    {
                        schSchedulerAvailability.Visible = false;
                    }
                }
                else
                {
                    schMeetingScheduler.Visible = true;
                    schSchedulerAvailability.Visible = false;
                }
            }
            else
            {
                schMeetingScheduler.Visible = false;
                schSchedulerAvailability.Visible = false;
            }

            tblScheduleAttendee.Style.Clear();
            tblScheduleAttendee.Style.Add("display", "none");
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #region ddlEventName_SelectedIndexChanged
    /// <summary>
    /// Event to bind the Event Attendee list box with attendees of the selected Event.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlEventName_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            lstMeetingAttendee.Items.Clear();
            lstEventAttendee.Items.Clear();
            schMeetingScheduler.Rows.Clear();
            schMeetingScheduler.Dispose();
            schMeetingRoom.Rows.Clear();
            ListItem defaultItem = new ListItem("Select Meeting Room", "0");
            schMeetingScheduler.Visible = false;
            lblHost.Visible = false;
            lblHostLegend.Visible = false;
            schSchedulerAvailability.Visible = false;
            schMeetingRoom.Visible = false;
            ddlMeetingRoom.Items.Clear();
            ddlMeetingRoom.Items.Insert(0, defaultItem);
            Session["EventAttendee"] = null;
            Session["MeetingAttendeesDetails"] = null;
            Session["RoomDetails"] = null;
            tblEventDetails.Style.Clear();
            tblEventDetails.Style.Add("display", "none");

            if (ddlEventName.SelectedIndex != 0)
            {
                int eventId = Convert.ToInt32(ddlEventName.SelectedValue);

                // Binding Event Attendee listbox.
                EventAttendee eventAttendee = new EventAttendee();
                eventAttendee.EventID = eventId;
                CCollection loColl = new CCollection(eventAttendee, true);
                DataTable dtEventAttendee = loColl.DataTable;
                Session["EventAttendee"] = dtEventAttendee;
                bool isThereRoomForEvent = true;

                // Bind the room details for the selected event (highlighting the blocked datetime span for each meeting in the selected event).
                isThereRoomForEvent = BindRoomDetails(eventId);

                if (isThereRoomForEvent)
                {
                    if (dtEventAttendee.Rows.Count > 0)
                    {
                        lstEventAttendee.DataSource = dtEventAttendee;
                        lstEventAttendee.DataBind();
                    }
                    else
                    {
                        csvMeeting.ErrorMessage = Meeting.NoAttendeesForEvent;
                        csvMeeting.IsValid = false;
                    }
                }

                CMTEvent eventsColl = new CMTEvent();
                eventsColl.CurrentEventAdministratorGUI = CurrentUser.GUI;
                loColl = new CCollection(eventsColl, true);
                DataTable dtEventList = loColl.DataTable;
                DataRow drEventDetails = (dtEventList.Select("EventID = " + eventId)[0]);
                lblEventStartDateValue.Text = drEventDetails["StartDate"].ToString();
                lblEventEndDateValue.Text = drEventDetails["EndDate"].ToString();
                lblEventTimeZoneValue.Text = drEventDetails["UTCTimeZoneName"].ToString();
                tblEventDetails.Style.Clear();
                tblEventDetails.Style.Add("display", "block");

                schMeetingScheduler.Visible = false;
                lblHost.Visible = false;
                lblHostLegend.Visible = false;
                schSchedulerAvailability.Visible = false;
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

    #region ddlSearchEvent_SelectedIndexChanged
    /// <summary>
    /// Event to bind the Event Attendee list box with attendees of the selected Event.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlSearchEvent_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            Session["EventAttendee"] = null;
            Session["MeetingAttendeesDetails"] = null;
            Session["RoomDetails"] = null;

            if (ddlSearchEvent.SelectedIndex != 0)
            {
                int eventId = Convert.ToInt32(ddlSearchEvent.SelectedValue);

                // Binding Event Attendee listbox.
                EventAttendee eventAttendee = new EventAttendee();
                eventAttendee.EventID = eventId;
                CCollection loColl = new CCollection(eventAttendee, true);
                DataTable dtEventAttendee = loColl.DataTable;

                Room roomForEvent = new Room();
                roomForEvent.EventID = eventId;
                loColl = new CCollection(roomForEvent, true);
                DataTable dtEventRoomDetails = loColl.DataTable;
                DataTable dtDistinctRooms = dtEventRoomDetails.DefaultView.ToTable(true, "RoomID", "RoomNameNumber");

                // Bind the room drop down.
                ListItem defaultItem = new ListItem("Select Meeting Room", "0");
                ddlSearchMeetingRoom.DataSource = dtDistinctRooms;
                ddlSearchMeetingRoom.DataBind();
                ddlSearchMeetingRoom.Items.Insert(0, defaultItem);
                ddlSearchMeetingRoom.SelectedIndex = 0;

                btnSearch_Click(this.ddlEventName, EventArgs.Empty);
            }
            else
            {
                ListItem defaultItem = new ListItem("Select Meeting Room", "0");
                ddlSearchMeetingRoom.Items.Clear();
                ddlSearchMeetingRoom.Items.Insert(0, defaultItem);
                wgSearchMeeting.DataSource = null;
                wgSearchMeeting.DataBind();
                wgSearchMeeting.Visible = false;
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #region BindRoomDetails
    /// <summary>
    /// Method to bind the room details for the selected event.
    /// </summary>
    /// <param name="eventId"></param>
    /// <returns></returns>
    private bool BindRoomDetails(int eventId)
    {
        try
        {
            bool isThereRoomForEvent = true;
            DataTable dtEventRoomDetails = new DataTable();
            CCollection loColl;
            Room roomForEvent = new Room();
            roomForEvent.EventID = eventId;
            loColl = new CCollection(roomForEvent, true);
            dtEventRoomDetails = loColl.DataTable;
            Session["RoomDetails"] = dtEventRoomDetails;
            DataTable dtEventAttendee = new DataTable();

            if (dtEventRoomDetails.Rows.Count > 0)
            {
                EventAttendee eventAttendee = new EventAttendee();
                eventAttendee.EventID = eventId;
                loColl = new CCollection(eventAttendee, true);
                dtEventAttendee = loColl.DataTable;
                Session["EventAttendee"] = dtEventAttendee;

                if (dtEventAttendee.Rows.Count > 0)
                {
                    DateTime dtEventStartDate = Convert.ToDateTime(dtEventAttendee.Rows[0]["StartDate"].ToString());
                    DateTime dtEventEndDate = Convert.ToDateTime(dtEventAttendee.Rows[0]["EndDate"].ToString());
                    schMeetingRoom.FirstDateTime = dtEventStartDate;
                    schMeetingRoom.LastDateTime = dtEventEndDate;
                    DataTable dtDistinctRooms = dtEventRoomDetails.DefaultView.ToTable(true, "RoomID", "RoomNameNumber");
                    int roomScheduleCount = 0;
                    int meetingId = 0;

                    // Checks whether meeting has to be edited or inserted.                   
                    if (hdnMeetingID.Value != "")
                        meetingId = Convert.ToInt32(hdnMeetingID.Value);

                    // Method to bind the room availability in scheduler tool.
                    roomScheduleCount = AddRoomSchedule(dtEventRoomDetails, dtDistinctRooms, roomScheduleCount, meetingId);

                    // Bind the room drop down.
                    ListItem defaultItem = new ListItem("Select Meeting Room", "0");
                    ddlMeetingRoom.DataSource = dtDistinctRooms;
                    ddlMeetingRoom.DataBind();
                    ddlMeetingRoom.Items.Insert(0, defaultItem);
                    ddlMeetingRoom.SelectedIndex = 0;
                    schMeetingRoom.Visible = true;
                    btnShowHideRoomSchedule.Disabled = false;
                }
            }
            else
            {
                csvMeeting.ErrorMessage = Meeting.NoRoomsForEvent;
                csvMeeting.IsValid = false;
                isThereRoomForEvent = false;
            }

            return isThereRoomForEvent;
        }
        catch (Exception)
        {
            throw;
        }
    }

    #region AddRoomSchedule
    /// <summary>
    /// Method to bind the room availability in scheduler tool.
    /// </summary>
    /// <param name="dtEventRoomDetails"></param>
    /// <param name="dtDistinctRooms"></param>
    /// <param name="roomScheduleCount"></param>
    /// <param name="meetingId"></param>
    /// <returns></returns>
    private int AddRoomSchedule(DataTable dtEventRoomDetails, DataTable dtDistinctRooms, int roomScheduleCount, int meetingId)
    {
        foreach (DataRow drEventRrom in dtDistinctRooms.Rows)
        {
            string roomId = drEventRrom["RoomID"].ToString();
            DataRow[] drRooms = (dtEventRoomDetails.Select("RoomID = " + roomId));

            string[] roomDetails = new string[8];
            roomDetails[0] = drRooms[0]["RoomNameNumber"].ToString();
            roomDetails[1] = drRooms[0]["Capacity"].ToString();
            roomDetails[2] = (((drRooms[0]["Projector"].ToString().ToLower()) == "true") ? "Yes" : "No");
            roomDetails[3] = (((drRooms[0]["ConfPhone"].ToString().ToLower()) == "true") ? "Yes" : "No");
            roomDetails[4] = (((drRooms[0]["FlipChart"].ToString().ToLower()) == "true") ? "Yes" : "No");
            roomDetails[5] = (((drRooms[0]["NetworkConnection"].ToString().ToLower()) == "true") ? "Yes" : "No");
            roomDetails[6] = (((drRooms[0]["WhiteBoard"].ToString().ToLower()) == "true") ? "Yes" : "No");
            roomDetails[7] = drRooms[0]["PhoneNumber"].ToString();

            ResourceRow newRoom = new ResourceRow(roomDetails);
            newRoom.AllowDrag = false;
            newRoom.AllowEdit = false;
            newRoom.ResourceID = roomDetails[0];
            schMeetingRoom.Rows.Add(newRoom);

            foreach (DataRow drRoom in drRooms)
            {
                if (drRoom["MeetingDate"] != null && drRoom["MeetingDate"].ToString().Trim() != string.Empty)
                {
                    DateTime dtRoomBookedStartDateTemp = Convert.ToDateTime(drRoom["MeetingStartTime"].ToString());
                    DateTime dtRoomookedEndDateTemp = Convert.ToDateTime(drRoom["MeetingEndTime"].ToString());
                    DateTime meetingDate = Convert.ToDateTime(drRoom["MeetingDate"].ToString());
                    DateTime dtRoomBookedStartDate = meetingDate.AddHours(dtRoomBookedStartDateTemp.Hour).AddMinutes(dtRoomBookedStartDateTemp.Minute);
                    DateTime dtRoomBookedEndDate = meetingDate.AddHours(dtRoomookedEndDateTemp.Hour).AddMinutes(dtRoomookedEndDateTemp.Minute);
                    string meetingName = drRoom["MeetingName"].ToString();
                    int bookedMeetingId = Convert.ToInt32(drRoom["MeetingID"].ToString());

                    // Set the room blocked date time span in scheduler.
                    if (bookedMeetingId != meetingId)
                    {
                        if (schMeetingRoom.FirstDateTime <= dtRoomBookedStartDate && schMeetingRoom.LastDateTime >= dtRoomBookedEndDate)
                        {
                            SchedulerItem schRoomSchedule = new SchedulerItem();
                            schRoomSchedule.ItemID = Meeting.RoomScheduleBlockedItemID + roomScheduleCount.ToString();
                            roomScheduleCount++;
                            schRoomSchedule.DateBegin = dtRoomBookedStartDate;
                            schRoomSchedule.DateEnd = dtRoomBookedEndDate;
                            schRoomSchedule.Allows = SchedulerItemAllows.None;
                            schRoomSchedule.DisplayText = Meeting.BookedScheduleTooltip + meetingName;
                            schRoomSchedule.Tooltip = Meeting.BookedScheduleTooltip + meetingName;
                            schRoomSchedule.Style.HorizontalAlign = HorizontalAlign.Center;
                            schRoomSchedule.Style.BackColor = Color.FromName("#D44942");
                            newRoom.Items.Add(schRoomSchedule);
                        }
                    }
                }
            }
        }

        return roomScheduleCount;
    }
    #endregion

    #endregion

    #endregion

    #region ddlMeetingRoom_SelectedIndexChanged
    /// <summary>
    /// Event to save the room capacity in a hidden field.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ddlMeetingRoom_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (ddlMeetingRoom.SelectedIndex != 0)
            {
                DataTable dtEventRoomDetails = new DataTable();
                string roomId = ddlMeetingRoom.SelectedValue;
                CCollection loColl;
                Room roomForEvent = new Room();
                int eventId = Convert.ToInt32(ddlEventName.SelectedValue);
                roomForEvent.EventID = eventId;
                loColl = new CCollection(roomForEvent, true);
                dtEventRoomDetails = loColl.DataTable;
                Session["RoomDetails"] = dtEventRoomDetails;

                DataRow[] drRooms = (dtEventRoomDetails.Select("RoomID = " + roomId));
                hdnSelectedRoomCapacity.Value = drRooms[0]["Capacity"].ToString();

                if (hdnAllCleasred.Value != "1")
                {
                    if (hdnMeetingAttendeeCount.Value == "")
                    {
                        schMeetingScheduler.Visible = false;

                        if (schSchedulerAvailability.Rows.Count == 0)
                        {
                            schSchedulerAvailability.Visible = false;
                        }
                    }
                    else
                    {
                        schMeetingScheduler.Visible = true;
                        schSchedulerAvailability.Visible = false;
                    }
                }
                else
                {
                    schMeetingScheduler.Visible = false;
                    schSchedulerAvailability.Visible = false;
                }
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #region rblMeetingStatus_SelectedIndexChanged
    /// <summary>
    /// Event to disable the 'Schedule' button if the meeting is cancelled.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void rblMeetingStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (rblMeetingStatus.SelectedIndex == 1)
            {
                schSchedulerAvailability.Rows.Clear();
                schSchedulerAvailability.Visible = false;
                schMeetingScheduler.Rows.Clear();
                schMeetingScheduler.Visible = false;
                btnSchedule.Enabled = false;
                btnCheckAvailability.Enabled = false;
                lblHost.Visible = false;
                lblHostLegend.Visible = false;

                tblScheduleAttendee.Style.Clear();
                tblScheduleAttendee.Style.Add("display", "none");
                tdMeetingScheduler.Style.Clear();
                tdMeetingScheduler.Style.Add("width", "100%");
                hdnMeetingAttendeeCount.Value = "";
            }
            else
            {
                schSchedulerAvailability.Rows.Clear();
                schSchedulerAvailability.Visible = false;

                if (lstMeetingAttendee.Items.Count > 0)
                {
                    btnSchedule.Enabled = true;
                    btnCheckAvailability.Enabled = true;
                    schMeetingScheduler.Visible = true;
                    lblHost.Visible = true;
                    lblHostLegend.Visible = true;
                }
                else
                {
                    btnSchedule.Enabled = false;
                    btnCheckAvailability.Enabled = false;
                    schMeetingScheduler.Rows.Clear();
                    schMeetingScheduler.Visible = false;
                    lblHost.Visible = false;
                    lblHostLegend.Visible = false;
                }
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #region btnCheckAvailability_Click
    /// <summary>
    /// Event to check the availability of the attendee for the selected meeting time span.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCheckAvailability_Click(object sender, EventArgs e)
    {
        try
        {
            tblScheduleAttendee.Style.Clear();
            tblScheduleAttendee.Style.Add("display", "none");
            tdMeetingScheduler.Style.Clear();
            tdMeetingScheduler.Style.Add("width", "100%");
            CCollection loColl;
            schSchedulerAvailability.Rows.Clear();
            schMeetingScheduler.Rows.Clear();
            schMeetingScheduler.Visible = false;
            lblHost.Visible = false;
            lblHostLegend.Visible = false;
            DataTable dtEventAttendee = new DataTable();
            int eventId = Convert.ToInt32(ddlEventName.SelectedValue);

            EventAttendee eventAttendee = new EventAttendee();
            eventAttendee.EventID = eventId;
            loColl = new CCollection(eventAttendee, true);
            dtEventAttendee = loColl.DataTable;
            Session["EventAttendee"] = dtEventAttendee;


            int meetingId = 0;

            if (hdnMeetingID.Value != "")
                meetingId = Convert.ToInt32(hdnMeetingID.Value);

            DateTime dtEventStartDate = Convert.ToDateTime(dtEventAttendee.Rows[0]["StartDate"].ToString());
            DateTime dtEventEndDate = Convert.ToDateTime(dtEventAttendee.Rows[0]["EndDate"].ToString());
            DataTable dtMeetingAttendeeDetails = new DataTable();

            MeetingAttendee meetingAttendeeDetails = new MeetingAttendee();
            meetingAttendeeDetails.EventID = eventId;
            loColl = new CCollection(meetingAttendeeDetails, true);
            dtMeetingAttendeeDetails = loColl.DataTable;
            Session["MeetingAttendeesDetails"] = dtMeetingAttendeeDetails;

            schSchedulerAvailability.FirstDateTime = dtEventStartDate;
            schSchedulerAvailability.LastDateTime = dtEventEndDate;

            if (dtEventAttendee.Rows.Count > 0)
                if (lstMeetingAttendee.Items.Count > 0)
                {
                    int attendeeCount = 0;

                    foreach (ListItem lstSelectedAttendee in lstMeetingAttendee.Items)
                    {
                        DataRow[] drAttendeeDetailsNew = (dtEventAttendee.Select("EventAttendeeID = " + lstSelectedAttendee.Value));

                        if (drAttendeeDetailsNew.Length > 0)
                        {
                            DataRow drAttendeeDetails = drAttendeeDetailsNew[0];
                            string attendeeName = drAttendeeDetails["Name"].ToString();

                            ResourceRow newAttendee = new ResourceRow(attendeeName);
                            newAttendee.AllowDrag = false;
                            newAttendee.AllowEdit = false;
                            newAttendee.ResourceID = attendeeName;
                            schSchedulerAvailability.Rows.Add(newAttendee);

                            // Set the attendee non available time span in scheduler (this binds as per the available start and end date given by the attendee during creation of event).
                            attendeeCount = BindAttendeeNonAvailableTimeSpan(schSchedulerAvailability, attendeeCount, drAttendeeDetails, newAttendee, "#D44942");

                            // Set the attendee blocked time span in scheduler (this binds as per the time span of other meeting for which the attendee is booked).
                            attendeeCount = BindAttendeeBookedTimeSpan(meetingId, dtMeetingAttendeeDetails, attendeeCount, lstSelectedAttendee, newAttendee);
                        }
                    }

                    schSchedulerAvailability.Visible = true;
                }
                else
                {
                    csvMeetingAttendee.ErrorMessage = Meeting.MeetingNoAttendeeError;
                    csvMeetingAttendee.IsValid = false;
                    schSchedulerAvailability.Visible = false;
                }
        }
        catch (Exception)
        {
            throw;
        }
    }

    #region BindAttendeeBookedTimeSpan
    /// <summary>
    /// Method to set the attendee blocked time span in scheduler.
    /// </summary>
    /// <param name="meetingId"></param>
    /// <param name="dtMeetingAttendeeDetails"></param>
    /// <param name="attendeeCount"></param>
    /// <param name="lstSelectedAttendee"></param>
    /// <param name="newAttendee"></param>
    /// <returns></returns>
    private int BindAttendeeBookedTimeSpan(int meetingId, DataTable dtMeetingAttendeeDetails, int attendeeCount, ListItem lstSelectedAttendee, ResourceRow newAttendee)
    {
        DataRow[] drMeetingAttendeeDetails = dtMeetingAttendeeDetails.Select("EventAttendeeID = " + lstSelectedAttendee.Value);

        foreach (DataRow drMeetingAttendeeDetail in drMeetingAttendeeDetails)
        {
            DateTime dtAttendeeBookedStartDateTemp = Convert.ToDateTime(drMeetingAttendeeDetail["AttendeeStartTime"].ToString());
            DateTime dtAttendeeBookedEndDateTemp = Convert.ToDateTime(drMeetingAttendeeDetail["AttendeeEndTime"].ToString());
            DateTime meetingDate = Convert.ToDateTime(drMeetingAttendeeDetail["MeetingDate"].ToString());
            DateTime dtAttendeeBookedStartDate = meetingDate.AddHours(dtAttendeeBookedStartDateTemp.Hour).AddMinutes(dtAttendeeBookedStartDateTemp.Minute);
            DateTime dtAttendeeBookedEndDate = meetingDate.AddHours(dtAttendeeBookedEndDateTemp.Hour).AddMinutes(dtAttendeeBookedEndDateTemp.Minute);
            string meetingName = drMeetingAttendeeDetail["MeetingName"].ToString();
            int bookedMeetingId = Convert.ToInt32(drMeetingAttendeeDetail["MeetingID"].ToString());

            if (schSchedulerAvailability.FirstDateTime <= dtAttendeeBookedStartDate && schSchedulerAvailability.LastDateTime >= dtAttendeeBookedEndDate)
            {
                if (bookedMeetingId != meetingId)
                {
                    DateTime schStartTime = dtAttendeeBookedStartDate;
                    DateTime schEndTime = dtAttendeeBookedEndDate;
                    attendeeCount = AddSchedulerItem(attendeeCount, newAttendee, schStartTime, schEndTime, Meeting.BookedScheduleDisplayText, Meeting.BookedScheduleTooltip + meetingName, "#D44942");
                }
            }
            else if (schSchedulerAvailability.FirstDateTime <= dtAttendeeBookedStartDate && schSchedulerAvailability.LastDateTime >= dtAttendeeBookedStartDate)
            {
                DateTime schStartTime = dtAttendeeBookedStartDate;
                DateTime schEndTime = schSchedulerAvailability.LastDateTime;
                attendeeCount = AddSchedulerItem(attendeeCount, newAttendee, schStartTime, schEndTime, Meeting.BookedScheduleDisplayText, Meeting.BookedScheduleTooltip + meetingName, "#D44942");
            }
            else if (schSchedulerAvailability.FirstDateTime <= dtAttendeeBookedEndDate && schSchedulerAvailability.LastDateTime >= dtAttendeeBookedEndDate)
            {
                DateTime schStartTime = schSchedulerAvailability.FirstDateTime;
                DateTime schEndTime = dtAttendeeBookedEndDate;
                attendeeCount = AddSchedulerItem(attendeeCount, newAttendee, schStartTime, schEndTime, Meeting.BookedScheduleDisplayText, Meeting.BookedScheduleTooltip + meetingName, "#D44942");
            }
        }

        return attendeeCount;
    }
    #endregion

    #endregion

    #region BindAttendeeNonAvailableTimeSpan
    /// <summary>
    /// Method to set the attendee non available time span in scheduler.
    /// </summary>
    /// <param name="attendeeCount"></param>
    /// <param name="drAttendeeDetails"></param>
    /// <param name="newAttendee"></param>
    /// <returns></returns>
    private int BindAttendeeNonAvailableTimeSpan(Scheduler schScheduler, int attendeeCount, DataRow drAttendeeDetails, ResourceRow newAttendee, string schBackColor)
    {
        DateTime dtAttendeeAvailStartDate = new DateTime();
        DateTime dtAttendeeAvailEndDate = new DateTime();

        if (drAttendeeDetails["AvailStartDate"].ToString().Trim() != "")
            dtAttendeeAvailStartDate = Convert.ToDateTime(drAttendeeDetails["AvailStartDate"].ToString());
        else
            dtAttendeeAvailStartDate = schScheduler.FirstDateTime;

        if (drAttendeeDetails["AvailEndDate"].ToString().Trim() != "")
            dtAttendeeAvailEndDate = Convert.ToDateTime(drAttendeeDetails["AvailEndDate"].ToString());
        else
            dtAttendeeAvailEndDate = schScheduler.LastDateTime;

        if (drAttendeeDetails["AvailStartDate"].ToString().Trim() != "" || drAttendeeDetails["AvailEndDate"].ToString().Trim() != "")
        {
            if (schScheduler.FirstDateTime < dtAttendeeAvailStartDate && schScheduler.LastDateTime > dtAttendeeAvailEndDate)
            {
                DateTime schStartTime = schScheduler.FirstDateTime;
                DateTime schEndTime = dtAttendeeAvailStartDate;
                attendeeCount = AddSchedulerItem(attendeeCount, newAttendee, schStartTime, schEndTime, Meeting.AttendeeNonAvailabilityScheduleDisplayText, Meeting.AttendeeNonAvailabilityScheduleDisplayText, schBackColor);

                schStartTime = dtAttendeeAvailEndDate;
                schEndTime = schScheduler.LastDateTime;
                attendeeCount = AddSchedulerItem(attendeeCount, newAttendee, schStartTime, schEndTime, Meeting.AttendeeNonAvailabilityScheduleDisplayText, Meeting.AttendeeNonAvailabilityScheduleDisplayText, schBackColor);
            }
            else if (schScheduler.FirstDateTime < dtAttendeeAvailStartDate && schScheduler.LastDateTime > dtAttendeeAvailStartDate)
            {
                DateTime schStartTime = schScheduler.FirstDateTime;
                DateTime schEndTime = dtAttendeeAvailStartDate;
                attendeeCount = AddSchedulerItem(attendeeCount, newAttendee, schStartTime, schEndTime, Meeting.AttendeeNonAvailabilityScheduleDisplayText, Meeting.AttendeeNonAvailabilityScheduleDisplayText, schBackColor);
            }
            else if (schScheduler.FirstDateTime < dtAttendeeAvailEndDate && schScheduler.LastDateTime > dtAttendeeAvailEndDate)
            {
                DateTime schStartTime = dtAttendeeAvailEndDate;
                DateTime schEndTime = schScheduler.LastDateTime;
                attendeeCount = AddSchedulerItem(attendeeCount, newAttendee, schStartTime, schEndTime, Meeting.AttendeeNonAvailabilityScheduleDisplayText, Meeting.AttendeeNonAvailabilityScheduleDisplayText, schBackColor);
            }
            else if (schScheduler.LastDateTime <= dtAttendeeAvailStartDate || schScheduler.FirstDateTime >= dtAttendeeAvailEndDate)
            {
                DateTime schStartTime = schScheduler.FirstDateTime;
                DateTime schEndTime = schScheduler.LastDateTime;
                attendeeCount = AddSchedulerItem(attendeeCount, newAttendee, schStartTime, schEndTime, Meeting.AttendeeNonAvailabilityScheduleDisplayText, Meeting.AttendeeNonAvailabilityScheduleDisplayText, schBackColor);
            }
        }

        return attendeeCount;
    }
    #endregion

    #region AddSchedulerItem
    /// <summary>
    /// Method to add a scheduler item to scheduler tool.
    /// </summary>
    /// <param name="attendeeCount"></param>
    /// <param name="newAttendee"></param>
    /// <param name="meetingName"></param>
    /// <param name="schStartTime"></param>
    /// <param name="schEndTime"></param>
    /// <returns></returns>
    private static int AddSchedulerItem(int attendeeCount, ResourceRow newAttendee, DateTime schStartTime, DateTime schEndTime, string schDisplayText, string schTooltip, string schBackColor)
    {
        SchedulerItem schAttendeeBlockSchedule = new SchedulerItem();
        schAttendeeBlockSchedule.ItemID = Meeting.MeetingScheduleBlockedItemID + attendeeCount.ToString();
        attendeeCount++;
        schAttendeeBlockSchedule.DateBegin = schStartTime;
        schAttendeeBlockSchedule.DateEnd = schEndTime;
        schAttendeeBlockSchedule.DisplayText = schTooltip;
        schAttendeeBlockSchedule.Tooltip = schTooltip;
        schAttendeeBlockSchedule.Style.HorizontalAlign = HorizontalAlign.Center;
        schAttendeeBlockSchedule.Allows = SchedulerItemAllows.None;
        schAttendeeBlockSchedule.Style.BackColor = Color.FromName(schBackColor);
        newAttendee.Items.Add(schAttendeeBlockSchedule);

        return attendeeCount;
    }
    #endregion

    #region btnSchedule_Click
    /// <summary>
    /// Event to schedule the meeting time slot for the selected attendee.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSchedule_Click(object sender, EventArgs e)
    {
        try
        {
            tblScheduleAttendee.Style.Clear();
            tblScheduleAttendee.Style.Add("display", "none");
            tdMeetingScheduler.Style.Clear();
            tdMeetingScheduler.Style.Add("width", "100%");
            schSchedulerAvailability.Rows.Clear();
            schMeetingScheduler.Rows.Clear();
            hdnAllCleasred.Value = "0";

            string meetingName = txtMeetingName.Text.Trim();

            if (meetingName != String.Empty)
            {
                if (txtMeetingDate.Text.Trim() != String.Empty)
                {
                    DateTime meetingDate = Convert.ToDateTime(txtMeetingDate.Text.Trim());
                    DateTime meetingStartDateTime = meetingDate.AddHours(Convert.ToDouble(ddlMeetingStartTimeHrs.SelectedValue)).AddMinutes(Convert.ToDouble(ddlMeetingStartTimeMns.SelectedValue));

                    DataTable dtEventAttendee = new DataTable();
                    int eventId = Convert.ToInt32(ddlEventName.SelectedValue);

                    EventAttendee eventAttendee = new EventAttendee();
                    eventAttendee.EventID = eventId;
                    CCollection loColl = new CCollection(eventAttendee, true);
                    dtEventAttendee = loColl.DataTable;
                    Session["EventAttendee"] = dtEventAttendee;
                    int meetingId = 0;

                    if (hdnMeetingID.Value != "")
                        meetingId = Convert.ToInt32(hdnMeetingID.Value);

                    DateTime meetingEndDateTime = meetingDate.AddHours(Convert.ToDouble(ddlMeetingEndTimeHrs.SelectedValue)).AddMinutes(Convert.ToDouble(ddlMeetingEndTimeMns.SelectedValue));
                    DateTime eventStartDate = Convert.ToDateTime(dtEventAttendee.Rows[0]["StartDate"].ToString());
                    DateTime eventEndDate = Convert.ToDateTime(dtEventAttendee.Rows[0]["EndDate"].ToString());

                    // Checks whether meeting tie selected meeting time slot is within the event date time range.
                    if (meetingStartDateTime >= eventStartDate && meetingEndDateTime <= eventEndDate)
                    {
                        // Checks whether the meeting end time is greater than meeting start time.
                        if (meetingStartDateTime < meetingEndDateTime)
                        {
                            TimeSpan timeDiff = new TimeSpan(00, 15, 00);

                            // Checking time diff is atleast 15 min or not.
                            if ((meetingEndDateTime - meetingStartDateTime) >= timeDiff)
                            {
                                int meetingRoomId = Convert.ToInt32(ddlMeetingRoom.SelectedValue);
                                int meetingType = Convert.ToInt32(ddlMeetingType.SelectedValue);
                                bool isRoomAvailable = true;

                                // Checks room availability for the selected time slot.
                                isRoomAvailable = CheckRoomAvailability(eventId, meetingId, meetingStartDateTime, meetingEndDateTime, meetingRoomId, isRoomAvailable);

                                // Method to add the selected meeting attendees to scheduler tool.
                                isMeetingTimeChanged = false;
                                AddMeetingAttendee(meetingName, dtEventAttendee, eventId, meetingId, meetingDate, meetingStartDateTime, meetingEndDateTime, isRoomAvailable, meetingType);

                                btnSaveMeeting.Enabled = true;
                                btnSaveMeeting.OnClientClick = "return CheckRoomCapacity();";
                                //btnSendMail.Enabled = false;
                            }
                            else
                            {
                                csvMeetingSlot.ErrorMessage = Meeting.MeetingTimeSlotNotGreaterThan45;
                                csvMeetingSlot.IsValid = false;
                                schSchedulerAvailability.Visible = false;
                                schMeetingScheduler.Visible = false;
                                lblHost.Visible = false;
                                lblHostLegend.Visible = false;
                            }
                        }
                        else
                        {
                            csvMeetingSlot.ErrorMessage = Meeting.MeetingTimeSlotError;
                            csvMeetingSlot.IsValid = false;
                            schSchedulerAvailability.Visible = false;
                            schMeetingScheduler.Visible = false;
                            lblHost.Visible = false;
                            lblHostLegend.Visible = false;
                        }
                    }
                    else
                    {
                        csvMeetingDate.ErrorMessage = Meeting.MeetingDateOutOfEventDate;
                        csvMeetingDate.IsValid = false;
                        schSchedulerAvailability.Visible = false;
                        schMeetingScheduler.Visible = false;
                        lblHost.Visible = false;
                        lblHostLegend.Visible = false;
                    }

                }
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

    #region CheckRoomAvailability
    /// <summary>
    /// Method to check the room availability for the selected time slot.
    /// </summary>
    /// <param name="eventId"></param>
    /// <param name="meetingId"></param>
    /// <param name="meetingStartDateTime"></param>
    /// <param name="meetingEndDateTime"></param>
    /// <param name="meetingRoomId"></param>
    /// <param name="isRoomAvailable"></param>
    /// <returns></returns>
    private bool CheckRoomAvailability(int eventId, int meetingId, DateTime meetingStartDateTime, DateTime meetingEndDateTime, int meetingRoomId, bool isRoomAvailable)
    {
        DataTable dtEventRoomDetails = new DataTable();
        CCollection loColl;

        Room roomForEvent = new Room();
        roomForEvent.EventID = eventId;
        loColl = new CCollection(roomForEvent, true);
        dtEventRoomDetails = loColl.DataTable;
        Session["RoomDetails"] = dtEventRoomDetails;

        DataRow[] drRooms = (dtEventRoomDetails.Select("RoomID = " + meetingRoomId));

        // Checks whether room is available for the selected meeting slot.
        foreach (DataRow drRoom in drRooms)
        {
            if (drRoom["MeetingDate"] != null && drRoom["MeetingDate"].ToString().Trim() != string.Empty)
            {
                DateTime dtRoomBookedStartDateTemp = Convert.ToDateTime(drRoom["MeetingStartTime"].ToString());
                DateTime dtRoomookedEndDateTemp = Convert.ToDateTime(drRoom["MeetingEndTime"].ToString());
                DateTime bookedMeetingDate = Convert.ToDateTime(drRoom["MeetingDate"].ToString());
                DateTime dtRoomBookedStartDate = bookedMeetingDate.AddHours(dtRoomBookedStartDateTemp.Hour).AddMinutes(dtRoomBookedStartDateTemp.Minute);
                DateTime dtRoomBookedEndDate = bookedMeetingDate.AddHours(dtRoomookedEndDateTemp.Hour).AddMinutes(dtRoomookedEndDateTemp.Minute);
                int bookedMeetingId = Convert.ToInt32(drRoom["MeetingID"].ToString());

                if (meetingId != bookedMeetingId && ((meetingStartDateTime >= dtRoomBookedStartDate && meetingStartDateTime < dtRoomBookedEndDate)
                    || (meetingEndDateTime > dtRoomBookedStartDate && meetingEndDateTime <= dtRoomBookedEndDate)
                    || (meetingStartDateTime <= dtRoomBookedStartDate && meetingEndDateTime >= dtRoomBookedEndDate)))
                {
                    isRoomAvailable = false;
                    break;
                }
            }
        }

        return isRoomAvailable;
    }
    #endregion

    #region AddMeetingAttendee
    /// <summary>
    /// Method to add the selected meeting attendees to scheduler tool.
    /// </summary>
    /// <param name="meetingName"></param>
    /// <param name="dtEventAttendee"></param>
    /// <param name="eventId"></param>
    /// <param name="meetingId"></param>
    /// <param name="meetingDate"></param>
    /// <param name="meetingStartDateTime"></param>
    /// <param name="meetingEndDateTime"></param>
    /// <param name="isRoomAvailable"></param>
    private void AddMeetingAttendee(string meetingName, DataTable dtEventAttendee, int eventId, int meetingId, DateTime meetingDate, DateTime meetingStartDateTime, DateTime meetingEndDateTime, bool isRoomAvailable, int meetingType)
    {
        if (isRoomAvailable)
        {
            schMeetingScheduler.FirstDateTime = meetingDate.AddHours(0);
            schMeetingScheduler.LastDateTime = meetingDate.AddHours(+24);

            ResourceRow newMeeting = new ResourceRow(meetingName);
            newMeeting.AllowDrag = false;
            newMeeting.AllowEdit = false;
            newMeeting.ResourceID = meetingName;
            newMeeting.BodyStyle.BackColor = Color.Gainsboro;

            SchedulerItem schScheduleMeeting = new SchedulerItem();
            schScheduleMeeting.ItemID = Meeting.MeetingScheduleItemID;
            schScheduleMeeting.DateBegin = meetingStartDateTime;
            schScheduleMeeting.DateEnd = meetingEndDateTime;
            schScheduleMeeting.Allows = SchedulerItemAllows.None;
            schScheduleMeeting.Style.BackColor = Color.FromName("#6B696B");
            newMeeting.Items.Add(schScheduleMeeting);

            schMeetingScheduler.Rows.Add(newMeeting);
            DataTable dtMeetingAttendeeDetails = new DataTable();

            MeetingAttendee meetingAttendeeDetails = new MeetingAttendee();
            meetingAttendeeDetails.EventID = eventId;
            CCollection loColl = new CCollection(meetingAttendeeDetails, true);
            dtMeetingAttendeeDetails = loColl.DataTable;
            Session["MeetingAttendeesDetails"] = dtMeetingAttendeeDetails;

            if (dtEventAttendee.Rows.Count > 0)
            {
                if (lstMeetingAttendee.Items.Count > 0)
                {
                    int attendeeCount = 0;

                    // Add each attendee to the scheduler tool.
                    foreach (ListItem lstSelectedAttendee in lstMeetingAttendee.Items)
                    {
                        DataRow[] drAttendeeDetailsNew = (dtEventAttendee.Select("EventAttendeeID = " + lstSelectedAttendee.Value));

                        if (drAttendeeDetailsNew.Length > 0)
                        {
                            DataRow drAttendeeDetails = drAttendeeDetailsNew[0];

                            string attendeeName = drAttendeeDetails["Name"].ToString();
                            bool IsClient = drAttendeeDetails["IsClient"].ToString() == "" ? false : Convert.ToBoolean(drAttendeeDetails["IsClient"].ToString());
                            string attendeeLink = "<a href='#' id='btn" + attendeeCount.ToString() + "' onclick='ScheduleAttendee(this);' value='" + attendeeName + "' title='Click here to schedule time slot for this user.' >" + attendeeName + " </a>";
                            string isHost = "false";
                            string issues = "";
                            string countries = "";
                            isHost = (drAttendeeDetails["IsHost"].ToString().ToLower() == "true" ? "true" : "false");

                            if (Session["NewMeetingAttendeeDetials"] != null)
                            {
                                dtNewMeetingAttendeeDetials = (DataTable)Session["NewMeetingAttendeeDetials"];

                                foreach (DataRow drnewMeetingAttendeeDetails in dtNewMeetingAttendeeDetials.Rows)
                                {
                                    if (attendeeName == drnewMeetingAttendeeDetails["Name"].ToString())
                                    {
                                        isHost = (drnewMeetingAttendeeDetails["ISHost"].ToString().Trim() == "" ? false : Convert.ToBoolean(drnewMeetingAttendeeDetails["ISHost"].ToString())).ToString();
                                        issues = drnewMeetingAttendeeDetails["Issues"].ToString();
                                        countries = drnewMeetingAttendeeDetails["CountryRepresent"].ToString();
                                    }
                                }
                            }
                            //}

                            ResourceRow newAttendee = new ResourceRow(attendeeLink, lstSelectedAttendee.Value, isHost, IsClient.ToString(), issues, countries);
                            newAttendee.AllowDrag = false;
                            newAttendee.AllowEdit = false;
                            newAttendee.ResourceID = attendeeName;

                            if (isHost.ToLower() == "true")
                                newAttendee.HeaderStyle.BackColor = Color.FromName("#778899");

                            newMeeting.ChildRows.Add(newAttendee);

                            // Set the attendee non available time span in scheduler (this binds as per the available start and end date given by the attendee during creation of event).
                            attendeeCount = BindAttendeeNonAvailableTimeSpan(schMeetingScheduler, attendeeCount, drAttendeeDetails, newAttendee, "#D44942");

                            // Set the attendee blocked time span in scheduler (this binds as per the time span of other meeting for which the attendee is booked).
                            attendeeCount = MeetingAttendeeBlockSchedule(meetingStartDateTime, meetingEndDateTime, meetingId, dtMeetingAttendeeDetails, attendeeCount, lstSelectedAttendee, newAttendee, attendeeName);

                            if (meetingType == 1 && !isEditing)
                            {
                                chkIsHost.Checked = false;
                                txtIssues.Text = "";

                                // Method to add time slot for attendee.
                                AddAttendeeTimeSlot(attendeeName, meetingStartDateTime, meetingEndDateTime, Convert.ToBoolean(isHost), true);
                            }
                            else if (meetingType == 1 && isAttendeeEditing && !checkEdit)
                            {
                                chkIsHost.Checked = false;
                                txtIssues.Text = "";

                                // Method to add time slot for attendee.
                                AddAttendeeTimeSlot(attendeeName, meetingStartDateTime, meetingEndDateTime, Convert.ToBoolean(isHost), true);
                            }
                        }
                    }
                }

                hdnMeetingAttendeeCount.Value = lstMeetingAttendee.Items.Count.ToString();
                newMeeting.Expanded = true;
                schSchedulerAvailability.Visible = false;
                schMeetingScheduler.Visible = true;
                lblHost.Visible = true;
                lblHostLegend.Visible = true;
            }
            else
            {
                csvMeetingAttendee.ErrorMessage = Meeting.MeetingNoAttendeeError;
                csvMeetingAttendee.IsValid = false;
                schSchedulerAvailability.Visible = false;
                schMeetingScheduler.Visible = false;
                lblHost.Visible = false;
                lblHostLegend.Visible = false;
            }
        }
        else
        {
            csvMeetingRoom.ErrorMessage = Meeting.NoRoomsAvailableForMeeting;
            csvMeetingRoom.IsValid = false;
            schSchedulerAvailability.Visible = false;
            schMeetingScheduler.Visible = false;
            lblHost.Visible = false;
            lblHostLegend.Visible = false;
        }
    }

    #region MeetingAttendeeBlockSchedule
    /// <summary>
    /// Method to bind the attendee blocked time slot from other neetings.
    /// </summary>
    /// <param name="meetingId"></param>
    /// <param name="dtMeetingAttendeeDetails"></param>
    /// <param name="attendeeCount"></param>
    /// <param name="lstSelectedAttendee"></param>
    /// <param name="newAttendee"></param>
    /// <returns></returns>
    private int MeetingAttendeeBlockSchedule(DateTime attendeeStartTime, DateTime attendeeEndTime, int meetingId, DataTable dtMeetingAttendeeDetails, int attendeeCount, ListItem lstSelectedAttendee, ResourceRow newAttendee, string attendeeName)
    {
        DataRow[] drMeetingAttendeeDetails = dtMeetingAttendeeDetails.Select("EventAttendeeID = " + lstSelectedAttendee.Value);
        isEditing = false;
        bool isAdded = false;
        int meetingType = Convert.ToInt32(ddlMeetingType.SelectedValue);

        if (Session["NewMeetingAttendeeDetials"] != null)
        {
            dtNewMeetingAttendeeDetials = (DataTable)Session["NewMeetingAttendeeDetials"];
        }

        foreach (DataRow drMeetingAttendeeDetail in drMeetingAttendeeDetails)
        {
            int bookedMeetingId = Convert.ToInt32(drMeetingAttendeeDetail["MeetingID"].ToString());
            DateTime dtAttendeeBookedStartDateTemp = new DateTime();
            DateTime dtAttendeeBookedEndDateTemp = new DateTime();
            DateTime dtAttendeeBookedStartDate = new DateTime();
            DateTime dtAttendeeBookedEndDate = new DateTime();
            DateTime bookedMeetingDate = new DateTime();

            dtAttendeeBookedStartDateTemp = Convert.ToDateTime(drMeetingAttendeeDetail["AttendeeStartTime"].ToString());
            dtAttendeeBookedEndDateTemp = Convert.ToDateTime(drMeetingAttendeeDetail["AttendeeEndTime"].ToString());
            bookedMeetingDate = Convert.ToDateTime(drMeetingAttendeeDetail["MeetingDate"].ToString());
            dtAttendeeBookedStartDate = bookedMeetingDate.AddHours(dtAttendeeBookedStartDateTemp.Hour).AddMinutes(dtAttendeeBookedStartDateTemp.Minute);
            dtAttendeeBookedEndDate = bookedMeetingDate.AddHours(dtAttendeeBookedEndDateTemp.Hour).AddMinutes(dtAttendeeBookedEndDateTemp.Minute);

            if (meetingType == 1 && bookedMeetingId == meetingId)
                isAttendeeEditing = true;

            string bookedMeetingName = drMeetingAttendeeDetail["MeetingName"].ToString();
            string issues = drMeetingAttendeeDetail["Issues"].ToString();
            string cntRepresenting = drMeetingAttendeeDetail["CountryRepresent"].ToString();

            bool isHost = drMeetingAttendeeDetail["ISHost"].ToString().Trim() == "" ? false : Convert.ToBoolean(drMeetingAttendeeDetail["ISHost"].ToString());

            if (schMeetingScheduler.FirstDateTime <= dtAttendeeBookedStartDate && schMeetingScheduler.LastDateTime >= dtAttendeeBookedEndDate)
            {
                SchedulerItem schAttendeeBlockSchedule = new SchedulerItem();
                attendeeCount++;

                schAttendeeBlockSchedule.DateBegin = dtAttendeeBookedStartDate;
                schAttendeeBlockSchedule.DateEnd = dtAttendeeBookedEndDate;

                if (bookedMeetingId != meetingId)
                {
                    schAttendeeBlockSchedule.ItemID = Meeting.MeetingScheduleBlockedItemID + attendeeCount.ToString();
                    schAttendeeBlockSchedule.DisplayText = Meeting.BookedScheduleTooltip + bookedMeetingName;
                    schAttendeeBlockSchedule.Tooltip = Meeting.BookedScheduleTooltip + bookedMeetingName;
                    schAttendeeBlockSchedule.Style.HorizontalAlign = HorizontalAlign.Center;
                    schAttendeeBlockSchedule.Allows = SchedulerItemAllows.None;
                    schAttendeeBlockSchedule.Style.BackColor = Color.FromName("#D44942");
                    newAttendee.Items.Add(schAttendeeBlockSchedule);
                }
                else
                {
                    foreach (DataRow drnewMeetingAttendeeDetails in dtNewMeetingAttendeeDetials.Rows)
                    {
                        if (attendeeName == drnewMeetingAttendeeDetails["Name"].ToString())
                        {
                            schAttendeeBlockSchedule.ItemID = Meeting.MeetingScheduleAvailableItemID;
                            schAttendeeBlockSchedule.Style.BackColor = Color.FromName("#7BBF6A");

                            checkConflictWithMeetingTimeSlot(attendeeStartTime, attendeeEndTime, newAttendee, dtAttendeeBookedStartDate, dtAttendeeBookedEndDate, schAttendeeBlockSchedule);
                            isAdded = true;
                        }
                    }

                    if (!isAdded)
                    {
                        if (dtAttendeeBookedStartDate < attendeeStartTime)
                            schAttendeeBlockSchedule.DateBegin = attendeeStartTime;

                        if (dtAttendeeBookedEndDate > attendeeEndTime)
                            schAttendeeBlockSchedule.DateEnd = attendeeEndTime;

                        schAttendeeBlockSchedule.ItemID = Meeting.MeetingScheduleAvailableItemID;
                        schAttendeeBlockSchedule.Style.BackColor = Color.FromName("#7BBF6A");
                        newAttendee.Texts[2] = isHost.ToString();
                        newAttendee.Texts[4] = issues;
                        newAttendee.Texts[5] = cntRepresenting;

                        if (newAttendee.Texts[2].Trim().ToLower() == "true")
                            newAttendee.HeaderStyle.BackColor = Color.FromName("#778899");
                        else
                            newAttendee.HeaderStyle.BackColor = Color.LightGray;

                        isEditing = true;

                        checkConflictWithMeetingTimeSlot(attendeeStartTime, attendeeEndTime, newAttendee, dtAttendeeBookedStartDate, dtAttendeeBookedEndDate, schAttendeeBlockSchedule);
                    }
                }
            }
            else if (schMeetingScheduler.FirstDateTime <= dtAttendeeBookedStartDate && schMeetingScheduler.LastDateTime >= dtAttendeeBookedStartDate)
            {
                DateTime schStartTime = dtAttendeeBookedStartDate;
                DateTime schEndTime = schMeetingScheduler.LastDateTime;
                attendeeCount = AddSchedulerItem(attendeeCount, newAttendee, schStartTime, schEndTime, Meeting.BookedScheduleDisplayText, Meeting.BookedScheduleTooltip + bookedMeetingName, "#D44942");
            }
            else if (schMeetingScheduler.FirstDateTime <= dtAttendeeBookedEndDate && schMeetingScheduler.LastDateTime >= dtAttendeeBookedEndDate)
            {
                DateTime schStartTime = schMeetingScheduler.FirstDateTime;
                DateTime schEndTime = dtAttendeeBookedEndDate;
                attendeeCount = AddSchedulerItem(attendeeCount, newAttendee, schStartTime, schEndTime, Meeting.BookedScheduleDisplayText, Meeting.BookedScheduleTooltip + bookedMeetingName, "#D44942");
            }

            if (bookedMeetingId == meetingId)
            {
                newAttendee.Texts[2] = isHost.ToString();
                newAttendee.Texts[4] = issues;
                newAttendee.Texts[5] = cntRepresenting;

                if (newAttendee.Texts[2].Trim().ToLower() == "true")
                    newAttendee.HeaderStyle.BackColor = Color.FromName("#778899");
                else
                    newAttendee.HeaderStyle.BackColor = Color.LightGray;

                if (Session["NewMeetingAttendeeDetials"] != null)
                {
                    dtNewMeetingAttendeeDetials = (DataTable)Session["NewMeetingAttendeeDetials"];

                    foreach (DataRow drnewMeetingAttendeeDetails in dtNewMeetingAttendeeDetials.Rows)
                    {
                        if (attendeeName == drnewMeetingAttendeeDetails["Name"].ToString())
                        {
                            newAttendee.Texts[2] = (drnewMeetingAttendeeDetails["ISHost"].ToString().Trim() == "" ? false : Convert.ToBoolean(drnewMeetingAttendeeDetails["ISHost"].ToString())).ToString();
                            newAttendee.Texts[4] = drnewMeetingAttendeeDetails["Issues"].ToString();
                            newAttendee.Texts[5] = drnewMeetingAttendeeDetails["CountryRepresent"].ToString();
                        }
                    }
                }
            }
        }

        return attendeeCount;
    }

    private void checkConflictWithMeetingTimeSlot(DateTime meetingStartTime, DateTime meetingEndTime, ResourceRow newAttendee, DateTime attendeeBookedStartDate, DateTime attendeeBookedEndDate, SchedulerItem schAttendeeBlockSchedule)
    {
        bool skipSlot = false;
        if (attendeeBookedEndDate <= meetingStartTime)
        {
            // the currently scheduled slot's end time of attendee is less tha/= to meting start time 
            isMeetingTimeChanged = true;
            skipSlot = true;
        }
        else if (meetingEndTime <= attendeeBookedStartDate)
        {
            // meting end time is les/= to the currently scheduled slot's start time
            isMeetingTimeChanged = true;
            skipSlot = true;
        }

        if (!skipSlot)
        {
            //show time slot only if not met abve conditions
            newAttendee.Items.Add(schAttendeeBlockSchedule);
        }
    }
    #endregion

    #endregion

    #endregion

    #region Meeting/Event Attendee Navigation

    #region btnAllRight_Click
    /// <summary>
    /// Event to move all the attendees from the event list to meeting list.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAllRight_Click(object sender, EventArgs e)
    {
        try
        {
            if (lstEventAttendee.Items.Count > 0)
            {
                foreach (ListItem lsiEventAttendee in lstEventAttendee.Items)
                    lstMeetingAttendee.Items.Add(lsiEventAttendee);

                lstMeetingAttendee.SelectedIndex = 0;
            }

            // Clears all the error messages.
            lblMessage.Text = "";
            btnCheckAvailability.Enabled = true;
            btnSchedule.Enabled = true;         
            lstEventAttendee.Items.Clear();
            schSchedulerAvailability.Rows.Clear();
            schSchedulerAvailability.Visible = false;
            schMeetingScheduler.Rows.Clear();
            schMeetingScheduler.Visible = false;
            lblHost.Visible = false;
            lblHostLegend.Visible = false;
            tblScheduleAttendee.Style.Clear();
            tblScheduleAttendee.Style.Add("display", "none");
            tdMeetingScheduler.Style.Clear();
            tdMeetingScheduler.Style.Add("width", "100%");
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #region btnRight_Click
    /// <summary>
    /// Event to move the selected attendees from the event list to meeting list.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnRight_Click(object sender, EventArgs e)
    {
        try
        {
            if (lstEventAttendee.Items.Count > 0)
            {
                foreach (ListItem lsiEventAttendee in lstEventAttendee.Items)
                {
                    if (lsiEventAttendee.Selected)
                        lstMeetingAttendee.Items.Add(lsiEventAttendee);
                }

                foreach (ListItem lsiMeetingAttendee in lstMeetingAttendee.Items)
                    lstEventAttendee.Items.Remove(lsiMeetingAttendee);

                if (lstMeetingAttendee.Items.Count > 0)
                {
                    btnCheckAvailability.Enabled = true;
                    btnSchedule.Enabled = true;
                    lstMeetingAttendee.SelectedIndex = 0;
                }
                else
                {
                    btnCheckAvailability.Enabled = false;
                    btnSchedule.Enabled = false;
                }
            }

            // Clears all the error messages.            
            lblMessage.Text = "";
            schSchedulerAvailability.Rows.Clear();
            schSchedulerAvailability.Visible = false;
            schMeetingScheduler.Rows.Clear();
            schMeetingScheduler.Visible = false;
            lblHost.Visible = false;
            lblHostLegend.Visible = false;
            tblScheduleAttendee.Style.Clear();
            tblScheduleAttendee.Style.Add("display", "none");
            tdMeetingScheduler.Style.Clear();
            tdMeetingScheduler.Style.Add("width", "100%");
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #region btnLeft_Click
    /// <summary>
    /// Event to move the selected attendees from the meeting list back to event list.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnLeft_Click(object sender, EventArgs e)
    {
        try
        {
            if (lstMeetingAttendee.Items.Count > 0)
            {
                foreach (ListItem lsiMeetingAttendee in lstMeetingAttendee.Items)
                {
                    if (lsiMeetingAttendee.Selected)
                        lstEventAttendee.Items.Add(lsiMeetingAttendee);
                }

                foreach (ListItem lsiEventAttendee in lstEventAttendee.Items)
                    lstMeetingAttendee.Items.Remove(lsiEventAttendee);

                if (lstMeetingAttendee.Items.Count > 0)
                {
                    btnCheckAvailability.Enabled = true;
                    btnSchedule.Enabled = true;
                    lstMeetingAttendee.SelectedIndex = 0;
                }
                else
                {
                    btnCheckAvailability.Enabled = false;
                    btnSchedule.Enabled = false;
                }
            }

            // Clears all the error messages.           
            lblMessage.Text = "";
            schSchedulerAvailability.Rows.Clear();
            schSchedulerAvailability.Visible = false;
            schMeetingScheduler.Rows.Clear();
            schMeetingScheduler.Visible = false;
            lblHost.Visible = false;
            lblHostLegend.Visible = false;
            tblScheduleAttendee.Style.Clear();
            tblScheduleAttendee.Style.Add("display", "none");
            tdMeetingScheduler.Style.Clear();
            tdMeetingScheduler.Style.Add("width", "100%");
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #region btnAllLeft_Click
    /// <summary>
    /// Event to move all the attendees from the meeting list back to event list.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAllLeft_Click(object sender, EventArgs e)
    {
        try
        {
            if (lstMeetingAttendee.Items.Count > 0)
            {
                foreach (ListItem lsiMeetingAttendee in lstMeetingAttendee.Items)
                    lstEventAttendee.Items.Add(lsiMeetingAttendee);
            }

            // Clears all the error messages.          
            lblMessage.Text = "";
            btnCheckAvailability.Enabled = false;
            btnSchedule.Enabled = false;
            lstMeetingAttendee.Items.Clear();
            schSchedulerAvailability.Rows.Clear();
            schSchedulerAvailability.Visible = false;
            schMeetingScheduler.Rows.Clear();
            schMeetingScheduler.Visible = false;
            lblHost.Visible = false;
            lblHostLegend.Visible = false;
            tblScheduleAttendee.Style.Clear();
            tblScheduleAttendee.Style.Add("display", "none");
            tdMeetingScheduler.Style.Clear();
            tdMeetingScheduler.Style.Add("width", "100%");
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #endregion

    #region btnAttendeeScheduleSave_Click
    /// <summary>
    /// Event to add/update the start and end time for the attendee to the scheduler control.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAttendeeScheduleSave_Click(object sender, EventArgs e)
    {
        try
        {
            string attendeeName = txtAttendeeName.Value;
            DateTime attendeeStartTime = schMeetingScheduler.FirstDateTime.AddHours(Convert.ToDouble(ddlAttendeeStartTimeHrs.SelectedValue)).AddMinutes(Convert.ToDouble(ddlAttendeeStartTimeMns.SelectedValue));
            DateTime attendeeEndTime = schMeetingScheduler.FirstDateTime.AddHours(Convert.ToDouble(ddlAttendeeEndTimeHrs.SelectedValue)).AddMinutes(Convert.ToDouble(ddlAttendeeEndTimeMns.SelectedValue));
            DataTable dtMeetingAttendeeDetails = new DataTable();
            int eventId = Convert.ToInt32(ddlEventName.SelectedValue);

            // Method to add time slot for attendee.
            AddAttendeeTimeSlot(attendeeName, attendeeStartTime, attendeeEndTime, false, false);
        }
        catch (Exception)
        {
            throw;
        }
    }

    #region AddAttendeeTimeSlot
    /// <summary>
    /// Method to add time slot for attendee.
    /// </summary>
    /// <param name="attendeeName"></param>
    /// <param name="attendeeStartTime"></param>
    /// <param name="attendeeEndTime"></param>
    private void AddAttendeeTimeSlot(string attendeeName, DateTime attendeeStartTime, DateTime attendeeEndTime, bool isDefaultHost, bool isFirstTiem)
    {
        ResourceRow newMeeting = schMeetingScheduler.Rows[0];
        ResourceRow attendeeSchedule = newMeeting.ChildRows.Where(nm => nm.ResourceID.ToLower().Trim() == attendeeName.ToLower().Trim()).FirstOrDefault();

        bool isAlreadyScheduled = false;
        bool isAvailableTimeSlot = true;
        bool isWithinMeetingTimeSlot = false;
        string meetingName = txtMeetingName.Text;
        vlsMeeting.Visible = false;

        // Checks whether the selected time span is within the meeting start and end time.
        if ((attendeeStartTime >= newMeeting.Items[0].DateBegin && attendeeStartTime <= newMeeting.Items[0].DateEnd)
            && (attendeeEndTime >= newMeeting.Items[0].DateBegin && attendeeEndTime <= newMeeting.Items[0].DateEnd))
        {
            isWithinMeetingTimeSlot = true;
        }
        else
        {
            vlsSaveAttendeeSchedule.Visible = true;
            csvMeetingScheduler.ErrorMessage = Meeting.OutOfMeetingTimeRangeErrorMessage;
            csvMeetingScheduler.IsValid = false;
            upnlSaveAttendeeSchedule.Update();
        }

        if (isWithinMeetingTimeSlot)
        {
            // Checks whether selected time span is within available time slot of attendee.
            foreach (SchedulerItem schAttendeeSchedules in attendeeSchedule.Items)
            {
                if (schAttendeeSchedules.ItemID != Meeting.MeetingScheduleAvailableItemID)
                {
                    if ((attendeeStartTime >= schAttendeeSchedules.DateBegin && attendeeStartTime < schAttendeeSchedules.DateEnd)
                        || (attendeeEndTime > schAttendeeSchedules.DateBegin && attendeeEndTime <= schAttendeeSchedules.DateEnd)
                        || (attendeeStartTime <= schAttendeeSchedules.DateBegin && attendeeEndTime >= schAttendeeSchedules.DateEnd))
                    {
                        isAvailableTimeSlot = false;
                        vlsSaveAttendeeSchedule.Visible = true;
                        csvMeetingScheduler.ErrorMessage = Meeting.AttndeeNotAvailableErrorMessage;
                        csvMeetingScheduler.IsValid = false;
                        upnlSaveAttendeeSchedule.Update();
                        break;
                    }
                }
            }

            if (isAvailableTimeSlot)
            {
                lblMessage.Text = "";

                if (attendeeStartTime < attendeeEndTime)
                {
                    // Checks whether the attendee is already scheduled for the current meeting, if yes then updates the time slot else creates new scheduler item.
                    foreach (SchedulerItem schAttendeeSchedules in attendeeSchedule.Items)
                    {
                        if (schAttendeeSchedules.ItemID == Meeting.MeetingScheduleAvailableItemID)
                        {
                            if (!isAttendeeEditing)
                            {
                                schAttendeeSchedules.DateBegin = attendeeStartTime;
                                schAttendeeSchedules.DateEnd = attendeeEndTime;
                                attendeeSchedule.Texts[2] = chkIsHost.Checked.ToString();
                                attendeeSchedule.Texts[4] = txtIssues.Text.Trim();
                                attendeeSchedule.Texts[5] = getSelectedRepCountries();

                                if (attendeeSchedule.Texts[2].Trim().ToLower() == "true")
                                    attendeeSchedule.HeaderStyle.BackColor = Color.FromName("#778899");
                                else
                                    attendeeSchedule.HeaderStyle.BackColor = Color.LightGray;

                                if (Session["NewMeetingAttendeeDetials"] == null)
                                {
                                    if (dtNewMeetingAttendeeDetials != null)
                                    {
                                        DataRow dtNewAttendee = dtNewMeetingAttendeeDetials.NewRow();
                                        dtNewAttendee["Name"] = attendeeName;
                                        dtNewAttendee["ISHost"] = attendeeSchedule.Texts[2];
                                        dtNewAttendee["Issues"] = attendeeSchedule.Texts[4];
                                        dtNewAttendee["CountryRepresent"] = attendeeSchedule.Texts[5];

                                        dtNewMeetingAttendeeDetials.Rows.Add(dtNewAttendee);
                                        Session["NewMeetingAttendeeDetials"] = dtNewMeetingAttendeeDetials;
                                    }
                                }
                                else
                                {
                                    dtNewMeetingAttendeeDetials = (DataTable)Session["NewMeetingAttendeeDetials"];

                                    if (dtNewMeetingAttendeeDetials != null)
                                    {
                                        for (int iRow = 0; iRow < dtNewMeetingAttendeeDetials.Rows.Count; iRow++)
                                        {
                                            if (attendeeName == dtNewMeetingAttendeeDetials.Rows[iRow]["Name"].ToString())
                                            {
                                                dtNewMeetingAttendeeDetials.Rows[iRow].Delete();
                                            }
                                        }

                                        DataRow dtNewAttendee = dtNewMeetingAttendeeDetials.NewRow();
                                        dtNewAttendee["Name"] = attendeeName;
                                        dtNewAttendee["ISHost"] = attendeeSchedule.Texts[2];
                                        dtNewAttendee["Issues"] = attendeeSchedule.Texts[4];
                                        dtNewAttendee["CountryRepresent"] = attendeeSchedule.Texts[5];

                                        dtNewMeetingAttendeeDetials.Rows.Add(dtNewAttendee);
                                        Session["NewMeetingAttendeeDetials"] = dtNewMeetingAttendeeDetials;
                                    }
                                }
                            }
                            else
                            {
                                schAttendeeSchedules.DateBegin = attendeeStartTime;
                                schAttendeeSchedules.DateEnd = attendeeEndTime;
                            }

                            isAlreadyScheduled = true;
                            break;
                        }
                    }
                }
                else
                {
                    vlsSaveAttendeeSchedule.Visible = true;
                    csvMeetingScheduler.ErrorMessage = Meeting.AttendeeTimeSlotError;
                    csvMeetingScheduler.IsValid = false;
                    upnlSaveAttendeeSchedule.Update();
                }

                if (!isAlreadyScheduled)
                {
                    if (attendeeStartTime < attendeeEndTime)
                    {
                        SchedulerItem schAttendeeSchedule = new SchedulerItem();
                        schAttendeeSchedule.ItemID = Meeting.MeetingScheduleAvailableItemID;
                        schAttendeeSchedule.DateBegin = attendeeStartTime;
                        schAttendeeSchedule.DateEnd = attendeeEndTime;

                        if (isFirstTiem)
                        {
                            if (!isDefaultHost && attendeeSchedule.Texts[2] == "false")
                                attendeeSchedule.Texts[2] = chkIsHost.Checked.ToString();

                            if (attendeeSchedule.Texts[4] == "")
                                attendeeSchedule.Texts[4] = txtIssues.Text.Trim();

                            if (attendeeSchedule.Texts[5] == "")
                                attendeeSchedule.Texts[5] = getSelectedRepCountries();
                        }
                        else
                        {
                            if (!isDefaultHost)
                                attendeeSchedule.Texts[2] = chkIsHost.Checked.ToString();

                            attendeeSchedule.Texts[4] = txtIssues.Text.Trim();
                            attendeeSchedule.Texts[5] = getSelectedRepCountries();
                        }

                        if (attendeeSchedule.Texts[2].Trim().ToLower() == "true")
                            attendeeSchedule.HeaderStyle.BackColor = Color.FromName("#778899");
                        else
                            attendeeSchedule.HeaderStyle.BackColor = Color.LightGray;

                        schAttendeeSchedule.Style.BackColor = Color.FromName("#7BBF6A");

                        if (Session["NewMeetingAttendeeDetials"] == null)
                        {
                            if (dtNewMeetingAttendeeDetials != null)
                            {
                                DataRow dtNewAttendee = dtNewMeetingAttendeeDetials.NewRow();
                                dtNewAttendee["Name"] = attendeeName;
                                dtNewAttendee["ISHost"] = attendeeSchedule.Texts[2];
                                dtNewAttendee["Issues"] = attendeeSchedule.Texts[4];
                                dtNewAttendee["CountryRepresent"] = attendeeSchedule.Texts[5];

                                dtNewMeetingAttendeeDetials.Rows.Add(dtNewAttendee);
                                Session["NewMeetingAttendeeDetials"] = dtNewMeetingAttendeeDetials;
                            }
                        }
                        else
                        {
                            dtNewMeetingAttendeeDetials = (DataTable)Session["NewMeetingAttendeeDetials"];

                            if (dtNewMeetingAttendeeDetials != null)
                            {
                                for (int iRow = 0; iRow < dtNewMeetingAttendeeDetials.Rows.Count; iRow++)
                                {
                                    if (attendeeName == dtNewMeetingAttendeeDetials.Rows[iRow]["Name"].ToString())
                                    {
                                        dtNewMeetingAttendeeDetials.Rows[iRow].Delete();
                                    }
                                }

                                DataRow dtNewAttendee = dtNewMeetingAttendeeDetials.NewRow();
                                dtNewAttendee["Name"] = attendeeName;
                                dtNewAttendee["ISHost"] = attendeeSchedule.Texts[2];
                                dtNewAttendee["Issues"] = attendeeSchedule.Texts[4];
                                dtNewAttendee["CountryRepresent"] = attendeeSchedule.Texts[5];

                                dtNewMeetingAttendeeDetials.Rows.Add(dtNewAttendee);
                                Session["NewMeetingAttendeeDetials"] = dtNewMeetingAttendeeDetials;
                            }
                        }

                        attendeeSchedule.Items.Add(schAttendeeSchedule);
                    }
                    else
                    {
                        vlsSaveAttendeeSchedule.Visible = true;
                        csvMeetingScheduler.ErrorMessage = Meeting.AttendeeTimeSlotError;
                        csvMeetingScheduler.IsValid = false;
                        upnlSaveAttendeeSchedule.Update();
                    }
                }

                tblScheduleAttendee.Style.Clear();
                tblScheduleAttendee.Style.Add("display", "none");
                tdMeetingScheduler.Style.Clear();
                tdMeetingScheduler.Style.Add("width", "100%");

                ddlAttendeeStartTimeHrs.SelectedIndex = 0;
                ddlAttendeeStartTimeMns.SelectedIndex = 0;
                ddlAttendeeEndTimeHrs.SelectedIndex = 0;
                ddlAttendeeEndTimeMns.SelectedIndex = 0;
                txtIssues.Text = "";
                chkIsHost.Checked = false;
            }
        }
    }

    private string getSelectedRepCountries()
    {
        string repCountries = string.Empty;
        foreach (ListItem li in cblCountries.Items)
        {
            if (li.Selected)
            {
                repCountries += li.Value + ",";
            }
        }
        return repCountries.TrimEnd(',');
    }
    #endregion

    #endregion

    #region btnEditSchedule_Click
    /// <summary>
    /// Event to edit the scheduled time span of the attendee.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnEditSchedule_Click(object sender, EventArgs e)
    {
        try
        {
            ddlAttendeeStartTimeHrs.SelectedIndex = 0;
            ddlAttendeeStartTimeMns.SelectedIndex = 0;
            ddlAttendeeEndTimeHrs.SelectedIndex = 0;
            ddlAttendeeEndTimeMns.SelectedIndex = 0;
            chkIsHost.Checked = false;
            string attendeeName = txtAttendeeName.Value;
            lblAttendeeName.Text = txtAttendeeName.Value;

            // Gets the attendee start and end time and binds it to the appropriate dropdown.
            ResourceRow newMeeting = schMeetingScheduler.Rows[0];
            ResourceRow attendeeSchedule = newMeeting.ChildRows.Where(nm => nm.ResourceID.ToLower().Trim() == attendeeName.ToLower().Trim()).FirstOrDefault();
            foreach (SchedulerItem schAttendeeSchedule in attendeeSchedule.Items)
            {
                if (schAttendeeSchedule.ItemID == Meeting.MeetingScheduleAvailableItemID)
                {
                    DateTime attendeeStartTime = schAttendeeSchedule.DateBegin;
                    DateTime attendeeEndTime = schAttendeeSchedule.DateEnd;

                    ddlAttendeeStartTimeHrs.SelectedValue = (attendeeStartTime.Hour < 10 ? "0" + attendeeStartTime.Hour.ToString() : attendeeStartTime.Hour.ToString());
                    ddlAttendeeStartTimeMns.SelectedValue = attendeeStartTime.Minute.ToString();
                    ddlAttendeeEndTimeHrs.SelectedValue = (attendeeEndTime.Hour < 10 ? "0" + attendeeEndTime.Hour.ToString() : attendeeEndTime.Hour.ToString());
                    ddlAttendeeEndTimeMns.SelectedValue = attendeeEndTime.Minute.ToString();

                    if (attendeeSchedule.Texts[2].Trim().ToLower() == "true")
                        attendeeSchedule.HeaderStyle.BackColor = Color.FromName("#778899");
                    else
                        attendeeSchedule.HeaderStyle.BackColor = Color.LightGray;
                    break;
                }
              
            }

            chkIsHost.Checked = Convert.ToBoolean(attendeeSchedule.Texts[2]);
            txtIssues.Text = Convert.ToString(attendeeSchedule.Texts[4]);
            cblCountries.Items.Clear();
            if (!attendeeSchedule.Texts[5].Trim().Equals(string.Empty))
            {
                // set country selection from attendeeSchedule, which is a temp save location
                string[] countryReps = attendeeSchedule.Texts[5].Trim().Split(',');
                foreach (string country in countryReps)
                {
                    foreach (ListItem li in cblCountries.Items)
                    {
                        if (li.Value.Trim().Equals(country.Trim()))
                        {
                            li.Selected = true;
                        }
                    }
                }
            }
            lblCountries.Visible = false;

            // Check whether attendee is own Client or not, if yes disabling the Host checkbox.
            if (Convert.ToBoolean(attendeeSchedule.Texts[3]))
            {
                lblIsHost.Enabled = false;
                chkIsHost.Enabled = false;
            }
            else
            {
                lblIsHost.Enabled = true;
                chkIsHost.Enabled = true;

                MeetingAttendee meetingAttendee = new MeetingAttendee();
                DataTable dtRepCountries = meetingAttendee.GetMeetingAttendeeCntryMap(Convert.ToInt32(attendeeSchedule.Texts[1]), hdnMeetingID.Value == string.Empty ? 0 : Convert.ToInt32(hdnMeetingID.Value));

                if (dtRepCountries.Rows.Count > 0)
                {
                    lblCountries.Visible = true;

                    cblCountries.DataSource = dtRepCountries;
                    cblCountries.DataTextField = "Country";
                    cblCountries.DataValueField = "Country";
                    cblCountries.DataBind();

                    if (!attendeeSchedule.Texts[5].Trim().Equals(string.Empty))
                    {
                        // set country selection from attendeeSchedule, which is a temp save location
                        string[] countryReps = attendeeSchedule.Texts[5].Trim().Split(',');
                        foreach (string country in countryReps)
                        {
                            foreach (ListItem li in cblCountries.Items)
                            {
                                if (li.Value.Trim().Equals(country.Trim()))
                                {
                                    li.Selected = true;
                                }
                            }
                        }
                    }
                }
            }

            tblScheduleAttendee.Style.Clear();
            tblScheduleAttendee.Style.Add("display", "block");
            tdMeetingScheduler.Style.Clear();
            tdMeetingScheduler.Style.Add("width", (Convert.ToInt32(hdnSchedulerWidth.Value) - 315).ToString());
            schSchedulerAvailability.Visible = false;
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #region schMeetingScheduler_ItemDragged
    /// <summary>
    /// Event to validate the dragged schedule time span for each attendee.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void schMeetingScheduler_ItemDragged(object sender, SchedulerItemDraggedEventArgs e)
    {
        try
        {
            ResourceRow newMeeting = schMeetingScheduler.Rows[0];

            if (e.NewBegin < e.NewEnd)
            {
                if (e.NewBegin != e.OldBegin)
                {
                    // Checks whether the new start time is less then meeting start time, if yes then cancel the event.
                    if (newMeeting.Items[0].DateBegin > e.NewBegin)
                    {
                        e.Cancel = true;
                        vlsMeeting.Visible = false;
                        vlsSaveAttendeeSchedule.Visible = true;
                        csvMeetingAttendeeSchedule.ErrorMessage = Meeting.OutOfMeetingTimeRangeErrorMessage;
                        csvMeetingAttendeeSchedule.IsValid = false;
                        upnlSaveAttendeeSchedule.Update();
                    }
                }

                if (e.NewEnd != e.OldEnd)
                {
                    // Checks whether the new end time is greater then meeting end time, if yes then cancel the event.
                    if (newMeeting.Items[0].DateEnd < e.NewEnd)
                    {
                        e.Cancel = true;
                        vlsMeeting.Visible = false;
                        vlsSaveAttendeeSchedule.Visible = true;
                        csvMeetingAttendeeSchedule.ErrorMessage = Meeting.OutOfMeetingTimeRangeErrorMessage;
                        csvMeetingAttendeeSchedule.IsValid = false;
                        upnlSaveAttendeeSchedule.Update();
                    }
                }
            }
            else
            {
                e.Cancel = true;
                vlsMeeting.Visible = false;
                vlsSaveAttendeeSchedule.Visible = true;
                csvMeetingAttendeeSchedule.ErrorMessage = Meeting.AttendeeTimeSlotError;
                csvMeetingAttendeeSchedule.IsValid = false;
                upnlSaveAttendeeSchedule.Update();
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #region btnSaveMeeting_Click
    /// <summary>
    /// Event to insert/update the meeting details.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSaveMeeting_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            try
            {
                // Clears all the error messages.
                lblMessage.Text = "";

                // Get all the entered meeting details.
                int eventId;
                string meetingName;
                string meetingDescription;
                string meetingNote;
                int meetingTypeId;
                string meetingStartTime;
                string meetingEndTime;
                int roomId;
                string addnlRoomRequirements;
                string meetingLink;
                string clientName;
                bool feedbackMailSent;
                DateTime meetingDate;
                bool meetingDateChnaged = false;

                if (schMeetingScheduler.Rows.Count > 0)
                {
                    ResourceRow chkMeetingSchedule = schMeetingScheduler.Rows[0];

                    string chkStartTime = (chkMeetingSchedule.Items[0].DateBegin.Hour < 10 ? "0" + chkMeetingSchedule.Items[0].DateBegin.Hour.ToString() : chkMeetingSchedule.Items[0].DateBegin.Hour.ToString())
                        + ":" + (chkMeetingSchedule.Items[0].DateBegin.Minute < 10 ? "0" + chkMeetingSchedule.Items[0].DateBegin.Minute.ToString() : chkMeetingSchedule.Items[0].DateBegin.Minute.ToString());

                    string chkEndTime = (chkMeetingSchedule.Items[0].DateEnd.Hour < 10 ? "0" + chkMeetingSchedule.Items[0].DateEnd.Hour.ToString() : chkMeetingSchedule.Items[0].DateEnd.Hour.ToString())
                        + ":" + (chkMeetingSchedule.Items[0].DateEnd.Minute < 10 ? "0" + chkMeetingSchedule.Items[0].DateEnd.Minute.ToString() : chkMeetingSchedule.Items[0].DateEnd.Minute.ToString());

                    if (schMeetingScheduler.Rows.Count > 0)
                    {
                        GetEnteredMeetingDetails(out eventId, out meetingName, out meetingDescription, out meetingNote, out meetingTypeId, out meetingStartTime, out meetingEndTime, out roomId, out addnlRoomRequirements, out meetingLink, out feedbackMailSent, out meetingDate, out clientName);

                        if (meetingDate.ToShortDateString() != Convert.ToDateTime(txtMeetingDate.Text).ToShortDateString())
                            meetingDateChnaged = true;
                        else if (chkStartTime != (ddlMeetingStartTimeHrs.SelectedValue + ":" + ddlMeetingStartTimeMns.SelectedValue))
                            meetingDateChnaged = true;
                        else if (chkEndTime != (ddlMeetingEndTimeHrs.SelectedValue + ":" + ddlMeetingEndTimeMns.SelectedValue))
                            meetingDateChnaged = true;

                        if (!meetingDateChnaged)
                        {
                            // Get the attendee details with scheduled time span and creates an xml element to pass it to SP.
                            int attendeeScheduled;
                            XDocument xAttendeesdoc;
                            ResourceRow meetingSchedule;
                            bool attendeeSchdeduleValid;
                            GetEnteredMeetingAttendeeDetails(out attendeeScheduled, out xAttendeesdoc, out meetingSchedule, out attendeeSchdeduleValid);

                            // Method to save the meeting details.
                            if (attendeeSchdeduleValid)
                                SaveMeetingDetails(eventId, meetingName, meetingDescription, meetingNote, meetingTypeId, meetingStartTime, meetingEndTime, roomId, addnlRoomRequirements, meetingLink, feedbackMailSent, meetingDate, attendeeScheduled, xAttendeesdoc, meetingSchedule, clientName, e);
                        }
                        else
                        {
                            csvMeetingAttendeeSchedule.ErrorMessage = Meeting.MeetingDateChnagedError;
                            csvMeetingAttendeeSchedule.IsValid = false;
                            schSchedulerAvailability.Visible = false;
                            lblHost.Visible = false;
                            lblHostLegend.Visible = false;
                        }
                    }
                    else
                    {
                        csvMeetingAttendeeSchedule.ErrorMessage = Meeting.AllAttendeeNotScheduled;
                        csvMeetingAttendeeSchedule.IsValid = false;
                        schSchedulerAvailability.Visible = false;
                        schMeetingScheduler.Visible = false;
                        lblHost.Visible = false;
                        lblHostLegend.Visible = false;
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        else
        {
            schMeetingScheduler.Visible = false;
            schSchedulerAvailability.Visible = false;
        }
    }

    #region GetEnteredMeetingAttendeeDetails
    /// <summary>
    /// Method to get the entered meeting attendee details.
    /// </summary>
    /// <param name="attendeeScheduled"></param>
    /// <param name="xAttendeesdoc"></param>
    /// <param name="meetingSchedule"></param>
    private void GetEnteredMeetingAttendeeDetails(out int attendeeScheduled, out XDocument xAttendeesdoc, out ResourceRow meetingSchedule, out bool attendeeSchdeduleValid)
    {
        attendeeSchdeduleValid = true;
        string tempMeetingId = "*=+*`";
        attendeeScheduled = 0;
        XElement xAttendees;
        xAttendeesdoc = new XDocument();
        XElement xRoot = new XElement("MeetingAttendees");
        xAttendeesdoc.Add(xRoot);

        meetingSchedule = schMeetingScheduler.Rows[0];
        DateTime meetingStartTime = meetingSchedule.Items[0].DateBegin;
        DateTime meetingEndTime = meetingSchedule.Items[0].DateEnd;

        foreach (ResourceRow attendeeSchedule in meetingSchedule.ChildRows)
        {
            foreach (SchedulerItem schAttendeeSchedule in attendeeSchedule.Items)
            {
                if (schAttendeeSchedule.ItemID == Meeting.MeetingScheduleAvailableItemID)
                {
                    if (schAttendeeSchedule.DateBegin >= meetingStartTime && schAttendeeSchedule.DateEnd <= meetingEndTime)
                    {
                        xAttendees = new XElement("MeetingAttendee");
                        xAttendees.Add(new XElement("MeetingID", tempMeetingId));
                        xAttendees.Add(new XElement("EventAttendeeID", Convert.ToInt32(attendeeSchedule.Texts[1])));
                        xAttendees.Add(new XElement("StartTime", schAttendeeSchedule.DateBegin.Hour.ToString() + ":" + schAttendeeSchedule.DateBegin.Minute.ToString()));
                        xAttendees.Add(new XElement("EndTime", schAttendeeSchedule.DateEnd.Hour.ToString() + ":" + schAttendeeSchedule.DateEnd.Minute.ToString()));
                        xAttendees.Add(new XElement("ISHost", attendeeSchedule.Texts[2]));
                        xAttendees.Add(new XElement("Issues", attendeeSchedule.Texts[4]));
                        xAttendees.Add(new XElement("CountryRepresent", attendeeSchedule.Texts[5]));

                        if (attendeeSchedule.Texts[2].Trim().ToLower() == "true")
                            attendeeSchedule.HeaderStyle.BackColor = Color.FromName("#778899");
                        else
                            attendeeSchedule.HeaderStyle.BackColor = Color.LightGray;

                        xRoot.Add(xAttendees);
                        attendeeScheduled++;
                        break;
                    }
                    else
                    {
                        attendeeSchdeduleValid = false;
                        csvMeeting.ErrorMessage = Meeting.AttendeeOutOfMeetingTimeRangeErrorMessage;
                        csvMeeting.IsValid = false;
                        schSchedulerAvailability.Visible = false;
                        break;
                    }
                }
            }

            if (!attendeeSchdeduleValid)
                break;
        }
    }
    #endregion

    #region GetEnteredMeetingDetails
    /// <summary>
    /// Method to get the entered meeting details.
    /// </summary>
    /// <param name="eventId"></param>
    /// <param name="meetingName"></param>
    /// <param name="meetingDescription"></param>
    /// <param name="meetingNote"></param>
    /// <param name="meetingTypeId"></param>
    /// <param name="meetingStartTime"></param>
    /// <param name="meetingEndTime"></param>
    /// <param name="roomId"></param>
    /// <param name="addnlRoomRequirements"></param>
    /// <param name="feedbackMailSent"></param>
    /// <param name="meetingDate"></param>
    private void GetEnteredMeetingDetails(out int eventId, out string meetingName, out string meetingDescription, out string meetingNote, out int meetingTypeId, out string meetingStartTime, out string meetingEndTime, out int roomId, out string addnlRoomRequirements, out string meetingLink, out bool feedbackMailSent, out DateTime meetingDate, out string clientName)
    {
        ResourceRow meetingSchedule = schMeetingScheduler.Rows[0];
        eventId = Convert.ToInt32(ddlEventName.SelectedValue);
        meetingName = txtMeetingName.Text.Trim();
        //meetingName = meetingSchedule.Texts[0];
        meetingDescription = txtMeetingDescription.Text;
        meetingNote = txtMeetingNote.Text;
        meetingTypeId = Convert.ToInt32(ddlMeetingType.SelectedValue);
        meetingStartTime = meetingSchedule.Items[0].DateBegin.Hour.ToString() + ":" + meetingSchedule.Items[0].DateBegin.Minute.ToString();
        meetingEndTime = meetingSchedule.Items[0].DateEnd.Hour.ToString() + ":" + meetingSchedule.Items[0].DateEnd.Minute.ToString();
        roomId = Convert.ToInt32(ddlMeetingRoom.SelectedValue);
        addnlRoomRequirements = "";
        feedbackMailSent = false;
        meetingDate = Convert.ToDateTime(meetingSchedule.Items[0].DateBegin.Date);
        meetingLink = txtMeetingLink.Text.Trim();
        clientName = txtClientName.Text.Trim();
    }
    #endregion

    #region SaveMeetingDetails
    /// <summary>
    /// Method to save the meeting details.
    /// </summary>
    /// <param name="eventId"></param>
    /// <param name="meetingName"></param>
    /// <param name="meetingDescription"></param>
    /// <param name="meetingNote"></param>
    /// <param name="meetingTypeId"></param>
    /// <param name="meetingStartTime"></param>
    /// <param name="meetingEndTime"></param>
    /// <param name="roomId"></param>
    /// <param name="addnlRoomRequirements"></param>
    /// <param name="feedbackMailSent"></param>
    /// <param name="meetingDate"></param>
    /// <param name="attendeeScheduled"></param>
    /// <param name="xAttendeesdoc"></param>
    /// <param name="meetingSchedule"></param>
    private void SaveMeetingDetails(int eventId, string meetingName, string meetingDescription, string meetingNote, int meetingTypeId, string meetingStartTime, string meetingEndTime, int roomId, string addnlRoomRequirements, string meetingLink, bool feedbackMailSent, DateTime meetingDate, int attendeeScheduled, XDocument xAttendeesdoc, ResourceRow meetingSchedule, string clientName, EventArgs e)
    {
        // Checks whether all the selected attendee are scheduled for the meeting, if not don't save meeting details, throughs an error message to the user.
        if (attendeeScheduled == meetingSchedule.ChildRows.Count)
        {
            // To add new meeting.
            //if (Session["MeetingID"] == null)
            if (hdnMeetingID.Value == "")
            {
                // Db call to create meeting for the selected event.
                CreateMeeting(eventId, meetingName, meetingDescription, meetingNote, meetingTypeId, meetingStartTime, meetingEndTime, roomId, addnlRoomRequirements, meetingLink, feedbackMailSent, meetingDate, xAttendeesdoc, clientName);

                ddlEventName.SelectedValue = eventId.ToString();
                ddlEventName_SelectedIndexChanged(this.ddlEventName, EventArgs.Empty);
            }
            // To update meeting.
            else
            {
                // Db call to update meeting.
                UpdateMeeting(meetingName, meetingDescription, meetingNote, meetingTypeId, meetingStartTime, meetingEndTime, roomId, addnlRoomRequirements, meetingLink, meetingDate, xAttendeesdoc, clientName, e);
                Session["EventID"] = null;
            }
        }
        else
        {
            csvMeeting.ErrorMessage = Meeting.AllAttendeeNotScheduled;
            csvMeeting.IsValid = false;
            schSchedulerAvailability.Visible = false;
        }
    }
    #endregion

    #region UpdateMeeting
    /// <summary>
    /// Method to update the meeting details.
    /// </summary>
    /// <param name="meetingName"></param>
    /// <param name="meetingDescription"></param>
    /// <param name="meetingNote"></param>
    /// <param name="meetingTypeId"></param>
    /// <param name="meetingStartTime"></param>
    /// <param name="meetingEndTime"></param>
    /// <param name="roomId"></param>
    /// <param name="addnlRoomRequirements"></param>
    /// <param name="meetingDate"></param>
    /// <param name="xAttendeesdoc"></param>
    private void UpdateMeeting(string meetingName, string meetingDescription, string meetingNote, int meetingTypeId, string meetingStartTime, string meetingEndTime, int roomId, string addnlRoomRequirements, string meetingLink, DateTime meetingDate, XDocument xAttendeesdoc, string clientName, EventArgs e)
    {        
        int meetingId = Convert.ToInt32(hdnMeetingID.Value);
        int meetingStatusId = Convert.ToInt32(rblMeetingStatus.SelectedValue);
        lblMessage.ForeColor = Color.Red;       

        // Db call to create meeting for the selected event.
        Meeting meeting = new Meeting();
        meeting.MeetingID = meetingId;
        meeting.MeetingName = meetingName;
        meeting.MeetingDescr = meetingDescription;
        meeting.MeetingNote = meetingNote;
        meeting.MeetingTypeID = meetingTypeId;
        meeting.StartDate = meetingStartTime;
        meeting.EndDate = meetingEndTime;
        meeting.RoomID = roomId;
        meeting.AdditionalRoomReq = addnlRoomRequirements;
        meeting.MeetingDate = meetingDate;
        meeting.MeetingStatusID = meetingStatusId;
        meeting.MeetingLink = meetingLink;
        meeting.ClientName = clientName;
        meeting.Save();
        int retValue = 1;       

        // No need to update attendee details if the meeting is cancelled.
        if (meetingStatusId != 2)
        {
            string meetingAttendees = xAttendeesdoc.ToString();
            meetingAttendees = meetingAttendees.Replace("*=+*`", meetingId.ToString());

            if (retValue != -1)
            {
                // Db call to add the attendee for the meeting, then clears all the fields.
                MeetingAttendee updateMeetingAttndee = new MeetingAttendee();
                int retParam = updateMeetingAttndee.UpdateMeetingAttendee(meetingId, meetingAttendees);

                if (retParam == 1)
                {
                    lblMessage.Text = Meeting.MeetingUpdatedSuccessfully;
                    lblMessage.ForeColor = Color.ForestGreen;
                    ClearFields();
                    wgSearchMeeting.DataSource = null;
                    wgSearchMeeting.DataBind();
                    wgSearchMeeting.Visible = false;
                }
                else
                    lblMessage.Text = Meeting.FailedToUpdateAttendee;
            }
            else
                lblMessage.Text = Meeting.MeetingUpdationFailed;
        }
        else
        {
            lblMessage.Text = Meeting.MeetingUpdatedSuccessfully;
            lblMessage.ForeColor = Color.ForestGreen;
            ClearFields();
        }
    }
    #endregion

    #region CreateMeeting
    /// <summary>
    /// Method to create a new meeting for the selected event.
    /// </summary>
    /// <param name="eventId"></param>
    /// <param name="meetingName"></param>
    /// <param name="meetingDescription"></param>
    /// <param name="meetingNote"></param>
    /// <param name="meetingTypeId"></param>
    /// <param name="meetingStartTime"></param>
    /// <param name="meetingEndTime"></param>
    /// <param name="roomId"></param>
    /// <param name="addnlRoomRequirements"></param>
    /// <param name="feedbackMailSent"></param>
    /// <param name="meetingDate"></param>
    /// <param name="xAttendeesdoc"></param>
    private void CreateMeeting(int eventId, string meetingName, string meetingDescription, string meetingNote, int meetingTypeId, string meetingStartTime, string meetingEndTime, int roomId, string addnlRoomRequirements, string meetingLink, bool feedbackMailSent, DateTime meetingDate, XDocument xAttendeesdoc, string clientName)
    {
        Meeting meeting = new Meeting();
        meeting.EventID = eventId;
        meeting.MeetingName = meetingName;
        meeting.MeetingDescr = meetingDescription;
        meeting.MeetingNote = meetingNote;
        meeting.MeetingTypeID = meetingTypeId;
        meeting.StartDate = meetingStartTime;
        meeting.EndDate = meetingEndTime;
        meeting.RoomID = roomId;
        meeting.AdditionalRoomReq = addnlRoomRequirements;
        meeting.FeedBackMailSent = feedbackMailSent;
        meeting.MeetingDate = meetingDate;
        meeting.MeetingLink = meetingLink;
        meeting.ClientName = clientName;
        meeting.Save();
        int meetingId = meeting.MeetingID;

        lblMessage.ForeColor = Color.Red;
        string meetingAttendees = xAttendeesdoc.ToString();
        meetingAttendees = meetingAttendees.Replace("*=+*`", meetingId.ToString());

        if (meetingId != -1)
        {
            MeetingAttendee addMeetingAttndee = new MeetingAttendee();
            int retParam = addMeetingAttndee.AddMeetingAttendee(meetingAttendees);

            if (retParam == 1)
            {
                lblMessage.Text = Meeting.MeetingAddedSuccessfully;
                lblMessage.ForeColor = Color.ForestGreen;
                ClearFields();
            }
            else
                lblMessage.Text = Meeting.FailedToAddAttendee;
        }
        else
            lblMessage.Text = Meeting.MeetingAddedFailed;
    }
    #endregion

    #endregion

    #region btnSearch_Click
    /// <summary>
    /// Event to search the meeting details.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            wgSearchMeeting.PageIndex = 0;
            BindSearchDetails();
        }
        catch (Exception)
        {
            throw;
        }
    }

    #region BindSearchDetails
    /// <summary>
    /// Method to bind the meeting search details.
    /// </summary>
    private void BindSearchDetails()
    {
        string eventId = null;
        string meetingTypeId = null;
        string roomId = null;
        string meetingName = null;
        string dtMeetingDate = null;
        string meetingStatusId = null;

        if (ddlSearchEvent.SelectedIndex > 0)
            eventId = ddlSearchEvent.SelectedValue;

        if (ddlSearchMeetingType.SelectedIndex > 0)
            meetingTypeId = ddlSearchMeetingType.SelectedValue;

        if (ddlSearchMeetingRoom.SelectedIndex > 0)
            roomId = ddlSearchMeetingRoom.SelectedValue;

        if (txtSearchMeetingName.Text.Trim() != string.Empty)
            meetingName = txtSearchMeetingName.Text.Trim();

        if (txtSearchMeetingDate.Text.Trim() != string.Empty)
            dtMeetingDate = txtSearchMeetingDate.Text.Trim();

        if (rblSearchMeetingStatus.SelectedIndex != -1)
            meetingStatusId = rblSearchMeetingStatus.SelectedValue;
        else
        {
            rblSearchMeetingStatus.SelectedIndex = 0;
            meetingStatusId = "0";
        }

        Meeting searchForMeeting = new Meeting();
        DataTable dtSearchMeeting = searchForMeeting.GetSearchedMeetingDetails(eventId, meetingTypeId, roomId, meetingName, dtMeetingDate, meetingStatusId, CurrentUser.GUI);
        wgSearchMeeting.DataSource = dtSearchMeeting;
        wgSearchMeeting.DataBind();

        if (dtSearchMeeting.Rows.Count > 0)
        {
            wgSearchMeeting.Visible = true;
            lblNoMeetingFound.Visible = false;
        }
        else
        {
            wgSearchMeeting.Visible = false;
            lblNoMeetingFound.Visible = true;
        }
    }
    #endregion

    #endregion

    #region btnClearAll_Click
    /// <summary>
    /// Event for cancel meeting creation/updation and clears all the fields.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnClearAll_Click(object sender, EventArgs e)
    {
        try
        {
            ClearFields();
        }
        catch (Exception)
        {
            throw;
        }
    }
    #endregion

    #region btnClearSearch_Click
    /// <summary>
    /// Event to clear all the search fields.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnClearSearch_Click(object sender, EventArgs e)
    {
        try
        {
            ClearSearchFields();
        }
        catch (Exception)
        {
            throw;
        }
    }

    #region ClearSearchFields
    /// <summary>
    /// Method to clear all the search fields.
    /// </summary>
    private void ClearSearchFields()
    {
        ddlSearchMeetingRoom.Items.Clear();
        ListItem defaultItem = new ListItem("Select Meeting Room", "0");
        ddlSearchMeetingRoom.Items.Insert(0, defaultItem);
        ddlSearchMeetingRoom.SelectedValue = "0";
        ddlSearchEvent.SelectedValue = "0";
        ddlSearchMeetingType.SelectedValue = "0";
        txtSearchMeetingName.Text = "";
        txtSearchMeetingDate.Text = "";
        wgSearchMeeting.DataSource = null;
        wgSearchMeeting.DataBind();
        wgSearchMeeting.Visible = false;
        lblNoMeetingFound.Visible = false;
    }
    #endregion

    #endregion

    #region ClearFields
    /// <summary>
    /// Method to clear all the fields.
    /// </summary>
    private void ClearFields()
    {
        Session["NewMeetingAttendeeDetials"] = null;
        schMeetingRoom.Rows.Clear();
        schMeetingRoom.Visible = false;
        ddlMeetingRoom.Items.Clear();
        ListItem defaultItem = new ListItem("Select Meeting Room", "0");
        ddlMeetingRoom.Items.Insert(0, defaultItem);
        ddlMeetingRoom.SelectedIndex = 0;
        lstEventAttendee.Items.Clear();
        lstMeetingAttendee.Items.Clear();
        schSchedulerAvailability.Rows.Clear();
        schSchedulerAvailability.Visible = false;
        cblCountries.Items.Clear();
        schMeetingScheduler.Rows.Clear();

        schMeetingScheduler.Visible = false;
        lblHost.Visible = false;
        lblHostLegend.Visible = false;
        ddlAttendeeStartTimeHrs.SelectedIndex = 0;
        ddlAttendeeStartTimeMns.SelectedIndex = 0;
        ddlAttendeeEndTimeHrs.SelectedIndex = 0;
        ddlAttendeeEndTimeMns.SelectedIndex = 0;
        chkIsHost.Checked = false;
        lblAttendeeName.Text = "";
        ddlEventName.SelectedIndex = 0;
        ddlEventName.Enabled = true;
        ddlMeetingType.SelectedIndex = 0;
        txtMeetingName.Text = "";
        txtMeetingDate.Text = "";
        txtMeetingDescription.Text = "";
        txtMeetingNote.Text = "";
        txtMeetingLink.Text = "";
        txtClientName.Text = "";
        ddlMeetingStartTimeHrs.SelectedIndex = 0;
        ddlMeetingStartTimeMns.SelectedIndex = 0;
        ddlMeetingEndTimeHrs.SelectedIndex = 0;
        ddlMeetingEndTimeMns.SelectedIndex = 0;
        lblMeetingStatus.Visible = false;
        rblMeetingStatus.Visible = false;
        btnSaveMeeting.Enabled = false;
      
        btnShowHideRoomSchedule.Disabled = true;
        btnCheckAvailability.Enabled = false;
        btnSchedule.Enabled = false;      
        hdnMeetingID.Value = "";
        Session["EventAttendee"] = null;
        Session["MeetingAttendeesDetails"] = null;
        Session["RoomDetails"] = null;
        tblScheduleAttendee.Style.Clear();
        tblScheduleAttendee.Style.Add("display", "none");
        tdMeetingScheduler.Style.Clear();
        tdMeetingScheduler.Style.Add("width", "100%");
        tblEventDetails.Style.Clear();
        tblEventDetails.Style.Add("display", "none");
        hdnSelectedRoomCapacity.Value = "";
        hdnMeetingAttendeeCount.Value = "";
        hdnAllCleasred.Value = "1";
        txtIssues.Text = "";
    }
    #endregion

}
